Auto-Sort VBA Code Amendment

ahem27

New Member
Joined
Jan 26, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I found the below VBA code on another site that auto sorts the list in a certain Table/Column as you enter data.

It works fine (after renaming the table and column ranges to match my particular spreadsheet.)

However, I have more than one Table/Column I need this code to apply to.

Does anyone have any suggestions for how I can add additional Table/Column ranges to this code?

Thank you very much for any assistance!

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim SalesTable As ListObject
Dim SortCol As Range

Set SalesTable = ActiveSheet.ListObjects("Table_Name")
Set SortCol = Range("Table_Name[Column_Name]")

If Not Intersect(Target, SortCol) Is Nothing Then
    With SalesTable.Sort
        .SortFields.Clear
        .SortFields.Add Key:=SortCol, Order:=xlDescending
        .Header = xlYes
        .Apply
    End With
End If
End Sub
 
Last edited by a moderator:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Please try the following on a copy of your workbook
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Escape
    Application.EnableEvents = False
    Dim SalesTable As ListObject
    Dim SortCol As Range
    
    If Not Target.ListObject Is Nothing Then
        Set SalesTable = Target.ListObject
        Set SortCol = Intersect(Target.ListObject.HeaderRowRange, Target.EntireColumn)
        With SalesTable.Sort
            .SortFields.Clear
            .SortFields.Add Key:=SortCol, Order:=xlDescending
            .Header = xlYes
            .Apply
        End With
    End If

Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
 
Upvote 0
@ahem27
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0
Please try the following on a copy of your workbook
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Escape
    Application.EnableEvents = False
    Dim SalesTable As ListObject
    Dim SortCol As Range
   
    If Not Target.ListObject Is Nothing Then
        Set SalesTable = Target.ListObject
        Set SortCol = Intersect(Target.ListObject.HeaderRowRange, Target.EntireColumn)
        With SalesTable.Sort
            .SortFields.Clear
            .SortFields.Add Key:=SortCol, Order:=xlDescending
            .Header = xlYes
            .Apply
        End With
    End If

Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub


This is perfect!
Thank you so much!
 
Upvote 0
@ahem27
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

Understood. I will remember this for next time. Thank you!
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,213
Members
453,024
Latest member
Wingit77

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