I have recorded the macro below and I'd like it to work on all sheets / tables in the workbook. I've gathered that I need to replace "ActiveWorkbook.Worksheets("Ramp")" with "ActiveWorkbook.ActiveSheet.ListObjects" but I cannot figure how to get the sort to work.
macro that works on the sheet which I recorded it on:
Sub GateSort()
'
' GateSort Macro
' Automatic sorting by Terminal > Gate > Subordinate value
'
' Keyboard Shortcut: Ctrl+Shift+G
'
ActiveWorkbook.Worksheets("Ramp").ListObjects("Table1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Ramp").ListObjects("Table1").Sort.SortFields.Add2 _
Key:=Range("Table1[Sort Gate Leading]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Ramp").ListObjects("Table1").Sort.SortFields.Add2 _
Key:=Range("Table1[Sort Gate Number]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Ramp").ListObjects("Table1").Sort.SortFields.Add2 _
Key:=Range("Table1[Sort Gate Trailing]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Ramp").ListObjects("Table1").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
My current attempt:
Sub GateSort()
'
' GateSort Macro
' Automatic sorting by Terminal > Gate > Subordinate value
'
' Keyboard Shortcut: Ctrl+Shift+G
'
tName = ActiveCell.ListObject.Name
ActiveWorkbook.ActiveSheet.ListObjects(tName).Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.ListObjects(tName).Sort.SortFields.Add2 _
Key:=Range("tName[Sort Gate Leading]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.ActiveSheet.ListObjects(tName).Sort.SortFields.Add2 _
Key:=Range("tName[Sort Gate Number]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.ActiveSheet.ListObjects(tName).Sort.SortFields.Add2 _
Key:=Range("tName[Sort Gate Trailing]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.ListObjects(tName).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
I've been playing with variables as indicated above though I've not had success. This is all to avoid manually creating a multi-level sort when needed.