Question Details

No question body available.

Tags

excel vba

Answers (1)

August 5, 2025 Score: 3 Rep: 169,601 Quality: Medium Completeness: 80%

Here's a basic example you could adapt:

Sub ListSorter()

Dim ws As Worksheet, lo As ListObject, c As Range, noSelection As Boolean Dim f As String

Set ws = ActiveSheet 'or a specific worksheet Set lo = ws.ListObjects("MyList") 'table to be sorted

noSelection = True

With lo.Sort .SortFields.Clear

For Each c In ws.Range("sortfields").Cells 'loop over range with sort fields f = c.Value If Len(f) > 0 And f "Not used" Then .SortFields.Add2 Key:=lo.ListColumns(f).Range, Order:=xlAscending noSelection = False 'flag at least one field was chosen End If Next c

If noSelection Then 'did the user select any sort fields? MsgBox "No sort field(s) selected!" Exit Sub End If

'do the sort .Header = xlYes .MatchCase = False .Apply End With

End Sub

My example ListObject/Table and sort fields:

enter image description here