Forum

How To Sort Excel F...
 
Notifications
Clear all

How To Sort Excel File (2013) with 4 or 5 Keys in VBA

3 Posts
2 Users
0 Reactions
157 Views
(@apdiya)
Posts: 2
New Member
Topic starter
 

​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

 
Posted : 08/04/2022 2:19 am
(@debaser)
Posts: 837
Member Moderator
(@apdiya)
Posts: 2
New Member
Topic starter
 

Thanks for the answer. I am totally new here and out of interest to get solution, I posted the same query in more than 1 forum. Sorry this will not be repeated again. 

 
Posted : 09/04/2022 5:20 am
Share: