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 ListObjectDim columnToSortBy As RangeSet tbl = Sheets("PB").ListObjects("AI")Set columnToSortBy = tbl.ListColumns(9).Range'Sort tableWith tbl.Sort.SortFields.Clear.SortFields.Add columnToSortBy, xlDescending.Header = xlYes.MatchCase = False.ApplyEnd WithEnd Sub
Best Answer
First, you missed one parameter in .SortFields.Add method
. What you need is:
.SortFields.Add columnToSortBy, xlSortOnValues, xlDescending
Second, question from you comment. Try with this kind of References:
Set columnToSortBy = Range(tbl.Name & "[[#All],[column name here]]")