Can someone help me with the syntax

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,392
Office Version
  1. 365
  2. 2016
Platform
  1. 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?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top