Need to select all data in table, then filter the data

kwp004

Board Regular
Joined
Dec 27, 2016
Messages
93
I'm building a macro that filters a table. Rather than use Range A1:R1099, I want to select all of the data in the worksheet.


Range("A1:R1099").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"A2:A1099"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"R2:R1099"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"I2:I1099"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"E2:E1099"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"P1,Referral,C1", DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"F2:F1099"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"N1,E1,W1,C1,C2" _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:R1099")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Basically, rather than go to 1099 for the last row, I need to go to whatever the last row in the table is.
 
Upvote 0
How about
VBA Code:
   With ActiveWorkbook.Worksheets("Sheet1").Sort
      .SortFields.Clear
      .SortFields.Add2 Range("A1"), xlSortOnValues, xlAscending, , xlSortNormal
      .SortFields.Add2 Range("R1"), xlSortOnValues, xlAscending, , xlSortNormal
      .SortFields.Add2 Range("I1"), xlSortOnValues, xlAscending, , xlSortNormal
      .SortFields.Add2 Range("E1"), xlSortOnValues, xlAscending, "P1,Referral,C1", xlSortNormal
      .SortFields.Add2 Range("F1"), xlSortOnValues, xlAscending, "N1,E1,W1,C1,C2", xlSortNormal
      .SetRange Range("A1").CurrentRegion
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
   End With
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,802
Members
449,095
Latest member
m_smith_solihull

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