How to use ActiveSheet when sorting/filtering in VBA

jardenp

Active Member
Joined
May 12, 2009
Messages
373
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
I'm trying to create a macro to, among other things, sort, filter, and delete rows. From the macro recorder, I get:

Code:
Range("A2").Select
    ActiveWorkbook.Worksheets("TXHLPFKP381540").sort.SortFields.Clear
    ActiveWorkbook.Worksheets("TXHLPFKP381540").sort.SortFields.Add Key:=Range( _
        "A2:A31369"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("TXHLPFKP381540").sort
        .SetRange Range("A2:A31369")
        .Header = xlGuess
        .MatchCase = False
        .orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Columns("A:A").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$A$31369").AutoFilter Field:=1, Criteria1:="<>110*" _
        , Operator:=xlAnd, Criteria2:="<>111*"
    Range("A2").Select
    Selection.CurrentRegion.Select
    Selection.EntireRow.Delete
    Selection.AutoFilter
    Range("A1").Select
The reports I will run this on will have different sheet names and ranges, so I want to make the code non-sheet name and non-range specific. I suspect I would use "ActiveSheet" but I'm not sure where I would use it. Should I replace "Worksheets("TXHLPFKP381540")" with "ActiveSheet"? I tried that and it didn't work, but I probably didn't do it right.

As to the range, my normal attack is to replace the given range--here "Range("A2:A31369")"--with "Range("A2:A" & Range("A2").End(xlDown).Row)". Would this allow the macro to work on report with fewer or more rows than the example I'm using to create the macro (i.e., 31369 rows)?

Thanks for any help you can provide.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try

Code:
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
With ActiveSheet.Sort
    .SetRange Range("A2:A" & LR)
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Columns("A").AutoFilter
ActiveSheet.Range("A1:A" & LR).AutoFilter Field:=1, Criteria1:="<>110*" _
    , Operator:=xlAnd, Criteria2:="<>111*"
Range("A2").CurrentRegion.EntireRow.Delete
ActiveSheet.AutoFilter
 
Upvote 0
Thanks VoG! Definitely not the first time you've helped me. Setting the extended range description as LR is a great idea. I use that repeatedly in my macros, so that will help in other projects too.

If I could ask one follow-up, the last line "Activesheet.Autofilter" was meant to remove the filter, leaving only the cells that started with "110" or "111", but it doesn't. Since my original macro didn't work, I never got to the end to notice it too didn't work. What is the VBA code for removing a filter?

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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