Hello. First time I post here in a long time. Thank you in advance for your precious help.
I have the following code. It was created to use a button to sort column C from 20 to lastRow and it does work.
I now want to use the same code to sort in function of column E instead of C so I change the C for an E in of ActiveSheet.Sort.SortFields.Add2 Key:=Range("C19:C" & lastRow...........
But the macro is still sorting the data in function of column C and not column E. What am I doing wrong.
Sub SortDateFDE()
Dim lastRow As Long
Dim columnFDE As String
Dim decoderFDE As String
Dim sortField As sortField
decoderFDE = "DecoderFDE"
columnFDE = "E"
lastRow = Sheets(decoderFDE).Cells(Rows.Count, columnFDE).End(xlUp).Row
' Get the current sort settings for the column
On Error Resume Next
Set sortField = ActiveSheet.Sort.SortFields(1)
On Error GoTo 0
If sortField Is Nothing Then
' If the column is not sorted, sort in ascending order
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add2 Key:=Range("E19:E" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
Else
' If the column is already sorted, reverse the sort order
If sortField.SortOn = xlSortOnValues Then
If sortField.Order = xlAscending Then
sortField.Order = xlDescending
Else
sortField.Order = xlAscending
End If
End If
End If
With ActiveSheet.Sort
.SetRange Range("C19:N" & lastRow) 'replace with the range of cells containing your table data
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
I have the following code. It was created to use a button to sort column C from 20 to lastRow and it does work.
I now want to use the same code to sort in function of column E instead of C so I change the C for an E in of ActiveSheet.Sort.SortFields.Add2 Key:=Range("C19:C" & lastRow...........
But the macro is still sorting the data in function of column C and not column E. What am I doing wrong.
Sub SortDateFDE()
Dim lastRow As Long
Dim columnFDE As String
Dim decoderFDE As String
Dim sortField As sortField
decoderFDE = "DecoderFDE"
columnFDE = "E"
lastRow = Sheets(decoderFDE).Cells(Rows.Count, columnFDE).End(xlUp).Row
' Get the current sort settings for the column
On Error Resume Next
Set sortField = ActiveSheet.Sort.SortFields(1)
On Error GoTo 0
If sortField Is Nothing Then
' If the column is not sorted, sort in ascending order
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add2 Key:=Range("E19:E" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
Else
' If the column is already sorted, reverse the sort order
If sortField.SortOn = xlSortOnValues Then
If sortField.Order = xlAscending Then
sortField.Order = xlDescending
Else
sortField.Order = xlAscending
End If
End If
End If
With ActiveSheet.Sort
.SetRange Range("C19:N" & lastRow) 'replace with the range of cells containing your table data
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub