Consulting

Results 1 to 4 of 4

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

  1. #1
    VBAX Newbie
    Joined
    Apr 2022
    Posts
    1
    Location

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

    ​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
    Last edited by Aussiebear; 04-07-2022 at 12:32 AM. Reason: Added code tags to supplied code

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Be as you wish to seem

  3. #3

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    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.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •