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.
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