Sorting Tables (ListObjects) with VBA

kakiebos

Board Regular
Joined
Jun 10, 2018
Messages
62
Hi All.

I have a sheet with a number of Tables in it that I would like to sort on a regular basis.

Currently I have the following code which works, but I'm curious to know if there is a easier way with looping to sort all the tables in the sheet at once. Any suggestions?

Code:
Sub Sort_Lists()
'To sort all the list in the Lists sheet.


  Dim Ws As Worksheet


Set Ws = shLists
  With Ws
    .Select
    'Job Category Sorting
    .Range("Job_Category_Table[[#headers],[Job Category]]").Select
    With .ListObjects("Job_Category_Table").Sort.SortFields
      .Clear
      .Add Key:=Range("Job_Category_Table[[#all], [Job category]]"), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    End With
    With Ws.ListObjects("Job_Category_Table").Sort
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
    End With
    
    'Job Number Information Sorting
    .Range("Job_Number[[#headers],[Job Number Information]]").Select
    With .ListObjects("Job_Number").Sort.SortFields
      .Clear
      .Add Key:=Range("Job_Number[[#all], [Job Number Information]]"), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    End With
    With Ws.ListObjects("Job_Number").Sort
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
    End With
    
      'Public Holiday Sorting
    .Range("PublicHolidays[[#headers],[Date]]").Select
    With .ListObjects("PublicHolidays").Sort.SortFields
      .Clear
      .Add Key:=Range("PublicHolidays[[#all], [Date]]"), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    End With
    With Ws.ListObjects("PublicHolidays").Sort
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
    End With
      
  End With 'Ws
     
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
How about
Code:
Sub kakiebos()
   Dim i As Long
   Dim ary As Variant
   
   ary = Array("Job_Category_Table", "Job category", "Job_Number", "Job Number Information")
   With Sheet1
      For i = 0 To UBound(ary) Step 2
         With .ListObjects(ary(i)).Sort
            .SortFields.Clear
            .SortFields.Add Key:=.Parent.ListColumns(ary(i + 1)).DataBodyRange, SortOn:=xlSortOnValues, _
                  Order:=xlAscending, DataOption:=xlSortNormal
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
         End With
      Next i
   End With
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,218
Members
448,554
Latest member
Gleisner2

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