PDA

View Full Version : How to Sort Excel File (2013) with 4 or 5 Keys in VBA



apdiya
04-07-2022, 12:01 AM
​I have a requirement to sort an Excel file (2013) with 4 keys / 5 keys. The following vba code is not working and at the same time not throwing any error too. Need help /guidance. Please feel free to edit to get the results. Thanks in advance.



Sub ToSortFile()
Dim lastRow, LastCol As Long
lastRow = Cells(Rows.Count, 2).End(xlUp).Row
LastCol = Cells(2, Columns.Count).End(xlToLeft).Column
Set source_data = Range(Cells(2, 2), Cells(lastRow, LastCol))
Set Ws = Sheets("Details")
With Ws.Sort
.SortFields.Clear
.SortFields.Add Key:=Ws.Range("B2"), Order:=xlAscending
.SortFields.Add Key:=Ws.Range("C2"), Order:=xlAscending
.SortFields.Add Key:=Ws.Range("K2"), Order:=xlAscending
.SortFields.Add Key:=Ws.Range("M2"), Order:=xlAscending
.SetRange Ws.Range("B3:U" & lastRow)
.Header = xlYes
.Apply
End With
End Sub

Aflatoon
04-07-2022, 01:15 AM
Also asked (and answered) here: https://stackoverflow.com/questions/71764082/sorting-4-keys-of-excel-file

Aflatoon
04-08-2022, 01:47 AM
And:
https://forum.ozgrid.com/forum/index.php?thread/1231341-how-to-sort-excel-file-2013-with-4-or-5-keys-in-vba
https://www.mrexcel.com/board/threads/how-to-sort-excel-file-2013-with-4-or-5-keys-in-vba.1201529/
https://www.myonlinetraininghub.com/excel-forum/vba-macros/how-to-sort-excel-file-2013-with-4-or-5-keys-in-vba

Aussiebear
04-08-2022, 01:59 AM
This thread is closed. Apdiya, you seemingly don't seem to care about either the rules of the forum, nor about those who you are asking for assistance from.