Forum Discussion

Stevy_M's avatar
Stevy_M
Copper Contributor
Jun 17, 2021
Solved

Auto sort in Excel based on multiple columns

Hello,

 

I've seen multiple solutions for how to use VBA codes to auto-sort an excel sheet when there's a change in data. All these solutions, however, are based on sorting by values of a SINGLE column.

Looking at my attached sample, is there any way to have the data updated automatically by sorting values by looking at Column B first, and then column C? (descending would be preferred).

 

Many thanks in advance!

 

  • Here's the VBA code I could put together that works:

    Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Not Intersect(Target, Range("B3:C500")) Is Nothing Then
    Columns("A:F").Sort Key1:=Range("B4"), Key2:=Range("C4"), _
    Order1:=xlDescending, Order2:=xlDescending, Header:=xlYes, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom
    End If
    End Sub

1 Reply

  • Stevy_M's avatar
    Stevy_M
    Copper Contributor
    Here's the VBA code I could put together that works:

    Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Not Intersect(Target, Range("B3:C500")) Is Nothing Then
    Columns("A:F").Sort Key1:=Range("B4"), Key2:=Range("C4"), _
    Order1:=xlDescending, Order2:=xlDescending, Header:=xlYes, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom
    End If
    End Sub

Resources