I have tried to sort a table using the following VBA code. The code does select the correct column, and the column filter does get a small arrow in it, indicating that it is sorted. But the rows do not sort. They stay unsorted.
What is wrong?
Sub SortTableTest()
Dim tbl As ListObject
Dim columnToSortBy As Range
Set tbl = Sheets("PB").ListObjects("AI")
Set columnToSortBy = tbl.ListColumns(9).Range
'Sort table
With tbl.Sort
.SortFields.Clear
.SortFields.Add columnToSortBy, xlDescending
.Header = xlYes
.MatchCase = False
.Apply
End With
End Sub
tableis often a misnomer in Excel a lot of people are likely looking for how to sort data in general: Sort data based on column