Scenario: I have a sheet and am trying to sort a part of it. Inside the sheet I have a dropdown list, which allows me to select the values (aaa, bbb, ccc). Each of those values represent a column and when selected, the code should sort the range by that column.
Problem: The process works for one of the values in the dropdown list, but not for the others (it runs, but nothing happens).
Code:
Sub ratecolumnssort()
Dim LastRow As Integer
Dim sortColumn As String, sortAgent As String
shtMonitoring.Activate
LastRow = shtMonitoring.Cells(shtMonitoring.rows.count, "B").End(xlUp).row
sortAgent = shtMonitoring.Cells(8, 9) ' this is where the dropdown with values aaa, bbb and ccc is
If sortAgent = "aaa" Then
sortColumn = "F"
ElseIf sortAgent = "bbb" Then
sortColumn = "H"
ElseIf sortAgent = "ccc" Then
sortColumn = "J"
End If
With ActiveSheet.sort
.SortFields.Add key:=Range(sortColumn & "11"), Order:=xlAscending
.SetRange Range("B11", "N" & LastRow )
.Header = xlYes
.Apply
End With
End Sub
Question: What am I doing wrong here?
ActiveSheet..AutoFilter.Sort.SortFields.Clearbefore each sorting?