

June 15, 2022

Hi. Im trying to sort a table, I tried recording a macro to get an idea but it produces loads of code just for one click
Any ideas how to sort a table (.ListObjects(1)) by column 3, then by column 4?


October 5, 2010

Hi,
Try this
Sub SortCols()
ActiveSheet.ListObjects("Table1").Sort.SortFields.Clear
Range("Table1").Sort Key1:=Range("Table1[[#All],[Col3]]"), Key2:=Range("Table1[[#All],[Col4]]"), Header:=xlYes
End Sub
Change Col3 and Col4 to the column names. Be default this sorts in Ascending order. If you want something else you can use the Order parameters e.g.
Sub SortCols()
ActiveSheet.ListObjects("Table1").Sort.SortFields.Clear
Range("Table1").Sort Key1:=Range("Table1[[#All],[Col3]]"), Key2:=Range("Table1[[#All],[Col4]]"), Header:=xlYes, Order1:=xlAscending, Order2:=xlDescending
End Sub
regards
Phil

Answers Post


Trusted Members
Moderators

November 1, 2018

1 Guest(s)
