VBA To Select All Visible Rows After Autofilter

honkin

Active Member
Joined
Mar 20, 2012
Messages
385
Office Version
  1. 2016
Platform
  1. MacOS
Hi

I recorded a macro of running an autofilter and then selecting all the visible rows and deleting. Here is the code it returned:

VBA Code:
Selection.AutoFilter
    ActiveSheet.Range("$A$1").CurrentRegion.AutoFilter Field:=21, Criteria1:="500"
    ActiveSheet.Range("$A$1").CurrentRegion.AutoFilter Field:=21, Criteria1:="=500" _
        , Operator:=xlOr, Criteria2:="="
    Rows("51:51").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp

It initially put a range in the code, but I have now resolved that issue of the variable number of rows in the daily file downloaded from a website, but I need the selection phase to select all visible rows except the header row and then delete. The rows will, of course, be non-contiguous, so that will be the issue

cheers
 
Hi Peter_SSs

I had a go at fixing all of that myself and it appears to work. Does anything in this look not quite right?

VBA Code:
Range("A1").CurrentRegion.Select
    With Selection
      .AutoFilter Field:=21, Criteria1:="=500", Operator:=xlOr, Criteria2:="="
      .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
      .AutoFilter
    End With
    Range("A1").CurrentRegion.Select
    With Selection
      .AutoFilter Field:=17, Criteria1:= _
        "#DIV/0!"
      .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
      .AutoFilter
    End With
    Dim LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range( _
        "A2:A" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveSheet.Sort.SortFields.Add2 Key:=Range( _
        "B2:B" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveSheet.Sort.SortFields.Add2 Key:=Range( _
        "U2:U" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("A2:U" & LR)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

Cheers
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Nothing looks obviously wrong and if it is working ... (y)
 
Upvote 0
Excellent mate. I just know that often the macro recorder puts things in a way that can be streamlined, but as you say, if it is working

Thanks so much for your help on the autofilter section

cheers
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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