Sort query

CookieMonster76

Board Regular
Joined
Apr 30, 2015
Messages
195
Hi

Searching around, i have found this as an efficient way to sort some data:

Worksheets("Sheet5").Sort.SortFields.Clear
Range("A4:M" & Cells(Rows.Count, "A").End(xlUp).Row).Sort Key1:=Range("L4:L" & Cells(Rows.Count, "A").End(xlUp).Row), Order1:=xlAscending, Header:=xlYes

I have then performed another action on it, and then need to re-sort it, but this time based on the criteria of 2 columns.

The macro recorder (amended for the range) give me this:

ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Add2 Key:=Range("B4:B" & Cells(Rows.Count, "A").End(xlUp).Row), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Add2 Key:=Range("D4:D" & Cells(Rows.Count, "A").End(xlUp).Row), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet5").Sort
.SetRange Range("A4:K" & Cells(Rows.Count, "A").End(xlUp).Row)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With


Is there a way to shrink it down in a similar fashion to the single column sort?

Thanks

Paul
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Do you want to sort cols A:K or A:M & do you want to sort on L,B & D or just B & D?
 
Upvote 0
Do you want to sort cols A:K or A:M & do you want to sort on L,B & D or just B & D?
The first part - There is data in columns A to M, and I want to sort on Column L.

Once that sort is done I do something else (as an aside, this part is deleting rows I don't need. If I don't do the above sort it takes ages to delete non sequential rows. Maybe there is a way to do this better?).

The second sort - There is now data only in cells A to K, and i want to sort by Col B, then by Col D.
 
Upvote 0
Ok, how about
VBA Code:
Sub CookieMonster()
   With Sheets("sheet5")
      With .Range("A4:K" & .Range("A" & Rows.Count).End(xlUp).Row)
         .Sort .Range("B4"), xlAscending, .Range("D4"), , xlAscending, , , xlYes
      End With
   End With
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub CookieMonster()
   With Sheets("sheet5")
      With .Range("A4:K" & .Range("A" & Rows.Count).End(xlUp).Row)
         .Sort .Range("B4"), xlAscending, .Range("D4"), , xlAscending, , , xlYes
      End With
   End With
End Sub
Thanks, perfect

For my learning/understanding. could i ask what the logic of what it's doing is? I get the use Sheet5, and the data in row A4:K, for the length of the data in col A, but what is the next line doing?

Thanks again

Paul
 
Upvote 0
It's doing the sort in the same way that your 1st code, I've just left out the argument names because I'm lazy. :)
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top