Consulting

Results 1 to 6 of 6

Thread: VBA Code needed to insert rows based on pricepoints

  1. #1

    VBA Code needed to insert rows based on pricepoints

    Hi,
    I have attached a cut down version of the data I'm trying to sort.
    Column A has product, and column B has a Sell price.
    I'm stuck on creating the code to look through the list of prices in column B, and insert blank rows to seperate the list into set pricegroups.
    Eg. 0 - 499 / 500 - 799 / 800 - 999 / 1000 - 1499 / 1500 - 1999 / 2000 - 2499 / 2500 - 2999 / 3000 - 3499 / 3500 - 3999 / 4000 - 4499 / 4500 - 4999

    Any help or advice would be greatly appreciated.
    Attached Files Attached Files

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I think this is what you wanted


    Option Explicit
    Sub InsertPricePoints()
        Dim aryPoints As Variant
        
        aryPoints = Array(0, 500, 800, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500)
        
        Dim iRow As Long
        
        Application.ScreenUpdating = False
        
        With ActiveSheet
            For iRow = .Range("B3").CurrentRegion.Cells(1, 1).End(xlDown).Row To .Range("B3").Row + 1 Step -1
                If Application.WorksheetFunction.Match(.Cells(iRow, 2).Value, aryPoints, 1) <> Application.WorksheetFunction.Match(.Cells(iRow - 1, 2).Value, aryPoints, 1) Then
                    .Rows(iRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                End If
            Next iRow
        End With
        Application.ScreenUpdating = True
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Quote Originally Posted by Paul_Hossler View Post
    I think this is what you wanted


    Option Explicit
    Sub InsertPricePoints()
        Dim aryPoints As Variant
        
        aryPoints = Array(0, 500, 800, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500)
        
        Dim iRow As Long
        
        Application.ScreenUpdating = False
        
        With ActiveSheet
            For iRow = .Range("B3").CurrentRegion.Cells(1, 1).End(xlDown).Row To .Range("B3").Row + 1 Step -1
                If Application.WorksheetFunction.Match(.Cells(iRow, 2).Value, aryPoints, 1) <> Application.WorksheetFunction.Match(.Cells(iRow - 1, 2).Value, aryPoints, 1) Then
                    .Rows(iRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                End If
            Next iRow
        End With
        Application.ScreenUpdating = True
    
    End Sub
    Thanks Paul, you're a champion. Love ya work.

  4. #4

    Delete rows with cells color formatted

    Hi,
    I am trying to find a code solution to delete entire row, if they have red color format (255) in Column A (column C has color formatted cells also, but they aren't considered to the rows to be deleted...only the color rows of column A are the basis to delete entire row.)
    Have tried numerous solutions, to no avail.

    Any help is greatly appreciated.

    Regards,
    Doug.
    Attached Files Attached Files

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    1. Very similar to first macro. When deleting rows, better to start at bottom and work up

    2. You used conditional formatting, but I went to the cell contents to check


    Option Explicit
    Sub DeleteRed()
        Dim iRow As Long
        
        Application.ScreenUpdating = False
        
        With ActiveSheet
            For iRow = .Range("A3").CurrentRegion.Cells(1, 1).End(xlDown).Row To .Range("A3").Row Step -1
                If InStr(UCase(.Cells(iRow, 1).Value), "RESALE") > 0 Or InStr(UCase(.Cells(iRow, 1).Value), "DEMO") > 0 Then
                    .Rows(iRow).Delete
                End If
            Next iRow
        End With
        Application.ScreenUpdating = True
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Quote Originally Posted by Paul_Hossler View Post
    1. Very similar to first macro. When deleting rows, better to start at bottom and work up

    2. You used conditional formatting, but I went to the cell contents to check


    Option Explicit
    Sub DeleteRed()
        Dim iRow As Long
        
        Application.ScreenUpdating = False
        
        With ActiveSheet
            For iRow = .Range("A3").CurrentRegion.Cells(1, 1).End(xlDown).Row To .Range("A3").Row Step -1
                If InStr(UCase(.Cells(iRow, 1).Value), "RESALE") > 0 Or InStr(UCase(.Cells(iRow, 1).Value), "DEMO") > 0 Then
                    .Rows(iRow).Delete
                End If
            Next iRow
        End With
        Application.ScreenUpdating = True
    End Sub


    Thanks Paul, worked a treat.

    Doug
    Last edited by Paul_Hossler; 08-26-2018 at 07:25 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •