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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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