Search code examples
excelvbasorting

VBA - Sort Multiple Columns


I am currently use sort but for only 1 column

   Sheets("BP").Range("A1:Q3000").Sort Key1:=Sheets("BP").Range("K1"), _
   Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
   Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,

How could I add another column on to here?

Thank you in advance.


Solution

  • As said suggested by @Shrotter you should use Key2 and Order2 parameters :

    Sheets("BP").Range("A1:Q3000").Sort Key1:=Sheets("BP").Range("K1"), _
    Order1:=xlAscending, _
    Key2:=Sheets("BP").Range("L1"), Order2:=xlAscending, _
    Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
    

    Key2 is set to Sheets("BP").Range("L1"), assuming you want to sort by column L after sorting by column K. Both are set to xlAscending for ascending order. If you want to sort in an other way you can use for example : xlDescending