VBA Code to Highlight/ Delete only Visible Rows of Filtered Results

lynnydd76

New Member
Joined
Dec 9, 2009
Messages
15
Hi Excel Masters,


I am working with a spreadsheet that does a data dump every month that is really messy, I'm trying to automate as much of the cleanup as possible.


The only thing I'm stuck on with the VBA code is how to delete visible rows instead of specific rows as the number of rows with data will change from month to month.


Any help would be much appreciated.


Code:
Sub Delete_Visible_Rows
'
'
    Cells.Select
    Selection.AutoFilter
    Range("A3").Select
    ActiveSheet.Range("$A$1:$J$2373").AutoFilter Field:=1, Criteria1:=Array( _
        "------------------*" _
        , _
        "- - *" _
        , _
        "========*" _
        , _
        "=======*" _
        , _
        "ACCNT*" _
        , "C CTR*", "COMPANY", "PROD   PROJ"), Operator _
        :=xlFilterValues
    Rows("5:2371").Select
    Selection.Delete Shift:=xlUp
    ActiveSheet.ShowAllData
    Range("A1").Select
End Sub


Code:
Sub Highlight_Visible_Rows()
'


    Cells.Select
    Selection.AutoFilter
    Range("A3").Select
    ActiveSheet.Range("$A$1:$J$3000").AutoFilter Field:=2, Criteria1:="=ATT :" _
        , Operator:=xlOr, Criteria2:="=CA"
    Rows("6:1857").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 10498160
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    ActiveSheet.ShowAllData
    Range("A1").Select
End Sub
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Maybe this UNTESTED

Code:
Sub Delete_Visible_Rows()
Dim myrange As Range
Application.ScreenUpdating = False
Set myrange = ActiveSheet.UsedRange
    With myrange
    .AutoFilter
    .AutoFilter Field:=1, Criteria1:=Array( _
        "------------------*" _
        , _
        "- - *" _
        , _
        "========*" _
        , _
        "=======*" _
        , _
        "ACCNT*" _
        , "C CTR*", "COMPANY", "PROD   PROJ"), Operator _
        :=xlFilterValues
    End With
myrange.Resize(myrange.Rows.Count - 1, 1).Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
ActiveSheet.ShowAllData
Range("A1").Select
myrange.AutoFilter
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,784
Members
449,124
Latest member
shreyash11

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