Filter columns (number of rows keeps changing)

gomes123

New Member
Joined
Jun 16, 2021
Messages
35
Office Version
  1. 2007
Platform
  1. Windows
I have a table starting from cell A2-L2. I would like to filter column J, from the largest to the smallest (descending order, the data is numerical) - starting from J2 (which is the heading), and the data is from J3 onwards. The end of the data changes, for now it is up to J1015 - which my macro works fine. But how would I modify the excel macro below to automatically filter all the data in column J if the last row of the table changes (for example to J1200)? Thanks

VBA Code:
Sub Macro1()


        Selection.AutoFilter
    ActiveSheet.Range("$A$2:$L$1015").AutoFilter Field:=9, Criteria1:="YES"
    ActiveWorkbook.Worksheets("Main").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Main").AutoFilter.Sort.SortFields.Add Key:=Range( _
        "J2:J1015"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Main").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You can find the 'last' used row and substitute that in. Take care if the data are already filtered as it returns the last visible row.

Sub Macro1()
dim LastRow as Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row

Selection.AutoFilter
ActiveSheet.Range("$A$2:$L$"& LastRow).AutoFilter Field:=9, Criteria1:="YES"
ActiveWorkbook.Worksheets("Main").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Main").AutoFilter.Sort.SortFields.Add Key:=Range( _
"J2:J" & LastRow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Main").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With


End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,176
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