Dynamically change Sort key value in table code

rbeaslin

New Member
Joined
Oct 30, 2008
Messages
16
Office Version
  1. 365
Platform
  1. Windows
I have a workbook with numerous sheets formatted identically with tables. First column is date, sorted ascending. I want to loop through the sheets, selecting the table by name (each has its own table name), sort on the "date" column. The code I have written to this point doesnt like the Key:= as is shown below. I have tried other methods and havent found a solution. I ran the macro recorder to match and the Key should be like "tablename[Date]". I cant get that to be dynamically changing in the code.

VBA Code:
Sub SortTables()
Dim tbl As ListObject
Dim sht As Worksheet

For Each sht In ThisWorkbook.Worksheets
    If sht.Name = "Data" Or sht.Name = "Summary" Or sht.Name = "Import" Or sht.Name = "Backup" Then
        'do Nothing
    Else
            For Each tbl In sht.ListObjects
                sht.Activate
                Worksheets(sht.Name).ListObjects(tbl.Name).Sort.SortFields.Clear
                Worksheets(sht.Name).ListObjects(tbl.Name).Sort.Add [COLOR=rgb(184, 49, 47)][B]Key:=tbl.Name & "[Date][/B][/COLOR]", SortOn:=xlSortOnValues, Order:=xlAscending
                With sht.ListObjects(tbl.Name).Sort.SortFields
                    .Header = xlYes
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With
            Next tbl
    End If
Next sht
End Sub


Thank you
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
How about
VBA Code:
   For Each tbl In sht.ListObjects
      sht.Activate
      tbl.Sort.SortFields.Clear
      tbl.Sort.SortFields.Add Key:=tbl.ListColumns("Date").Range, SortOn:=xlSortOnValues, Order:=xlAscending
      With tbl.Sort
         .Header = xlYes
         .Orientation = xlTopToBottom
         .SortMethod = xlPinYin
         .Apply
      End With
   Next tbl
 
Upvote 0
Exactly what I needed. I wasn't aware of the use/capabilities of .listcolumns.

Thank you, Fluff!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,952
Members
449,198
Latest member
MhammadishaqKhan

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