VBA help -- sort one column then another

dmj120

Active Member
Joined
Jan 5, 2010
Messages
311
Office Version
  1. 365
  2. 2019
  3. 2010
I'm not sure why the sort is not working. I would like to have this automatically redo the sort with each change in I2 (a list).

Can some please help to fix the sorting and how to automatically run it when I choose a different list option? I current have a button I assigned it to.

VBA Code:
Sub SortBidAmounts()
    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim sortCol As Range
    Dim mdlIDCol As Range
    Dim selectedBidAmt As String
    
    ' Set the worksheet and table
    Set ws = ThisWorkbook.Sheets("Sheet1")
    Set tbl = ws.ListObjects("ShoPricebook") ' the table name
    
    ' Get the selected bid amount column from cell I2 -- data validation = list
    selectedBidAmt = ws.Range("I2").Value
    
    ' Get sorting column ranges
    On Error Resume Next
    Set sortCol = tbl.ListColumns(selectedBidAmt).DataBodyRange
    Set mdlIDCol = tbl.ListColumns("RL Mdl.ID").DataBodyRange
    On Error GoTo 0

    ' Ensure both sorting columns exist
    If Not sortCol Is Nothing And Not mdlIDCol Is Nothing Then
        ' Perform the sorting
        With tbl.Sort
            .SortFields.Clear
            ' First, sort by the selected "Bid Amt" column (Descending)
            .SortFields.Add2 Key:=sortCol, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
            ' Then, sort by RL Mdl.ID (Ascending)
            .SortFields.Add2 Key:=mdlIDCol, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            
            ' Apply sorting settings
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .Apply
        End With
    Else
        MsgBox "Sorting column(s) not found. Ensure I2 contains a valid Bid Amt column name.", vbExclamation
    End If
End Sub
 

Forum statistics

Threads
1,226,834
Messages
6,193,216
Members
453,779
Latest member
C_Rules

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