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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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,214,958
Messages
6,122,475
Members
449,087
Latest member
RExcelSearch

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