Here's the code I've written to sort a table based on two columns then deselect the table once that's done. Is there a cleaner way to code this?
Sub SortTable()
' Sorts table
Worksheets("Data").ListObjects("Table").Sort.SortFields. _
Clear
Worksheets("Data").ListObjects("Table").Sort.SortFields. _
Add Key:=Range("Table[Date]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
Worksheets("Data").ListObjects("Table").Sort.SortFields. _
Add Key:=Range("Table[Info]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With Worksheets("Data").ListObjects("Table").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Clears table selection
With Worksheets("Data")
.Activate
.Range("A1").Select
End With
End Sub
You can do it tidier by moving your With
If you know the position of the columns to sort, then this method seems to clean things up.
For the above, the Date list column is 2 and Info is 4. If you don't know the column ordinals then you need to reference the columns from the parent worksheet.