Multiple Table Auto-sort on Tab Select

DaveGooch

New Member
Joined
Jan 14, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi, Appreciate that this is an old post - however this code has really worked well for me. I have a question though. Can it be modified to sort column 3 then column 2 on every table in the sheet?

Thanks,

Dave
 
Last edited by a moderator:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Welcome to the MrExcel board!

Best to start your own thread and link to an old one. I will do that for you in this case.
You'll likely get more people looking at a new thread. :)

See if this does what you want.

VBA Code:
Private Sub Worksheet_Activate()
  Dim tbl As ListObject
  
  Application.ScreenUpdating = False
  For Each tbl In ActiveSheet.ListObjects
    With tbl.Sort
      .SortFields.Clear
      .SortFields.Add Key:=tbl.DataBodyRange.Columns(3), SortOn:=xlSortOnValues, Order:=xlAscending
      .SortFields.Add Key:=tbl.DataBodyRange.Columns(2), SortOn:=xlSortOnValues, Order:=xlAscending
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
    End With
  Next tbl
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
You're a legend!! Thank you very much!

I adjusted it to do large to small in column 3 and small to large in column 2. Odd that it seemed to do it out of order? if I put it as descending column 3 first it didn't work.

VBA Code:
Private Sub Worksheet_Activate()
  Dim tbl As ListObject
  
  Application.ScreenUpdating = False
  For Each tbl In ActiveSheet.ListObjects
    With tbl.Sort
      .SortFields.Clear
      .SortFields.Add Key:=tbl.DataBodyRange.Columns(2), SortOn:=xlSortOnValues, Order:=xlAscending
      .SortFields.Add Key:=tbl.DataBodyRange.Columns(3), SortOn:=xlSortOnValues, Order:=xlDescending
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
    End With
  Next tbl
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi, when I save this file I often get an error that says I cannot save the file and need to save a new version - seems to be linked to an issue with this van code - can you see anything wrong with it? It would it be better if I assigned it to a button? (Any updates needed to make that work?). Thanks, Dave
 
Upvote 0
What is the file extension of the file?
 
Upvote 0
Hi, when I save this file I often get an error that says I cannot save the file and need to save a new version - seems to be linked to an issue with this van code - can you see anything wrong with it? It would it be better if I assigned it to a button? (Any updates needed to make that work?). Thanks, Dave
What is the file extension of the file?
.xlsm
 
Upvote 0
I can't see anything in that code which would cause a problem for an xlsm file.
What is the exact error message you get?
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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