VBA inserting 1 row above each previously filtered row

Jvalades

New Member
Joined
Jul 28, 2016
Messages
40
Hello hello again,

As stated above, I have a list of data that i filtered by color. After the filter has been done I want to sort through the leftover list of rows and add a row above each.

As of right now this is what I have to find the rows, the next step being to add a row above each row.

Code:
' Store the row count as a variable
Dim TheLastRow As Long
TheLastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row




'Apply filter for InStock Inv
 Rows("7:7").Select
 Selection.AutoFilter
 ActiveSheet.Range("$7:$31").AutoFilter Field:=2, Criteria1:=RGB(204, 255, _
        255), Operator:=xlFilterCellColor

Please advise and thank you.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Your details are a little sparse, but from your code you seem to be filtering rows 7-31, row 7 being the headings row. See if this does what you want:
Code:
Public Sub AutoFilter_and_Insert_Rows2()

    Dim lastRow As Long
    Dim filterRange As Range
    Dim numVisibleRows As Long, visibleRowNumbers() As Long
    Dim i As Long
    Dim cell As Range
    
    With ActiveSheet
        If .AutoFilterMode Then .AutoFilterMode = False
        
        'Autofilter from row 7 (headings row) to row 31
        
        .Rows("7:31").AutoFilter Field:=2, Criteria1:=RGB(204, 255, 255), Operator:=xlFilterCellColor
    
        'Create array to hold row numbers of visible rows, excluding headings row
            
        numVisibleRows = .AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1
        ReDim visibleRowNumbers(numVisibleRows - 1)
        
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).row
        Set filterRange = .Range("A8:A" & lastRow)
    
        i = 0
        For Each cell In filterRange.SpecialCells(xlCellTypeVisible)
            visibleRowNumbers(i) = cell.row
            i = i + 1
        Next
        
        'Loop backwards through visible row numbers and insert a row above each visible row, using format of visible row
        
        For i = UBound(visibleRowNumbers) To 0 Step -1
            .Rows(visibleRowNumbers(i)).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow 'xlFormatFromLeftOrAbove
        Next
    End With
   
End Sub
 
Upvote 0
Your details are a little sparse, but from your code you seem to be filtering rows 7-31, row 7 being the headings row. See if this does what you want:
Code:
Public Sub AutoFilter_and_Insert_Rows2()
    
        'Autofilter from row 7 (headings row) to row 31
        
        .Rows("7:31").AutoFilter Field:=2, Criteria1:=RGB(204, 255, 255), Operator:=xlFilterCellColor
    
   
End Sub


John_W
Can we make this filter a stored variable. For instance if I wanted to search more rows than just (7:31)

Maybe like:
Code:
Rows(TheLastRow).AutoFilter Field:=2, Criteria1:=RGB(204, 255, 255), Operator:=xlFilterCellColor
??

Is this possible
 
Upvote 0
John_W
Can we make this filter a stored variable. For instance if I wanted to search more rows than just (7:31)
Replace the AutoFilter with:
Code:
        'Autofilter from row 7 (column headings row) to last row with data in column A
        
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).row
        Set filterRange = .Rows("7:" & lastRow)
        filterRange.AutoFilter Field:=2, Criteria1:=RGB(204, 255, 255), Operator:=xlFilterCellColor
 
Upvote 0
Replace the AutoFilter with:
Code:
        'Autofilter from row 7 (column headings row) to last row with data in column A
        
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).row
        Set filterRange = .Rows("7:" & lastRow)
        filterRange.AutoFilter Field:=2, Criteria1:=RGB(204, 255, 255), Operator:=xlFilterCellColor

You are the best!

Thank you again for all of then help
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top