dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,392
- Office Version
- 365
- 2016
- Platform
- Windows
Code:
'declare variables
Dim combo As String 'Combo worksheet name
Dim SortTable As String 'Table name that needs sorting
'assign values to variables
combo = Worksheets("Home").Range("Q5") 'string in cell Q5 of Home worksheet
SortTable = Worksheets("Home").Range("R5")
ActiveWorkbook.Worksheets(combo).ListObjects(SortTable).Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets(combo).ListObjects(SortTable).Sort.SortFields. _
Add Key:=Range("july18[[#All],[Date of work]]"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("July 2018").ListObjects("July18").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
I am replacing all the references to July18 with a variable stored in a cell that has the correct reference to the table name. SortTable has the correct name in it and I have changed some of the references from july18 to SortTable but I wasn't sure how to change this line of code:
Code:
Add Key:=Range("july18[[#All],[Date of work]]"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
How would I change the july18 in the above code to reference the relative table name and not a fixed table name?