Help Understanding my Sort VBA code

keef2

Board Regular
Joined
Jun 30, 2022
Messages
185
Office Version
  1. 365
Platform
  1. Windows
Hi all,

The issue was I had a multilevel sort code that I wanted to sort by Job # then By Box # both in ascending order. However, for some reason I noticed in multiple areas that it was not properly sorted mostly because the data has a bunch of different style job #'s. Here is what was happening originally: (take a look at box 221 where the 1701 Cicero project was in the middle of 3XXX projects for no reason.
1661796416149.png


Here was my original code:

VBA Code:
Sub MultiLevelSort()

Worksheets("User Inputs").Sort.SortFields.Clear

Range("A2:G" & Range("A" & Rows.Count).End(xlUp).Row).Sort Key1:=Range("D2"), Key2:=Range("B2"), Header:=xlYes, _
    Order1:=xlAscending, Order2:=xlAscending, DataOption1:=xlSortTextAsNumbers
 
End Sub


My new code which applies the correct sorting is the following. Can anyone help explain why I needed to add the 2nd range sort to get this to work properly? Or is there a better way to code this? Thanks in advance!
VBA Code:
Sub MultiLevelSort()

Worksheets("User Inputs").Sort.SortFields.Clear

Range("A2:G" & Range("A" & Rows.Count).End(xlUp).Row).Sort Key1:=Range("D2"), Key2:=Range("B2"), Header:=xlYes, _
    Order1:=xlAscending, Order2:=xlAscending, DataOption1:=xlSortTextAsNumbers
   
Range("A2:G" & Range("A" & Rows.Count).End(xlUp).Row).Sort Key1:=Range("B2"), Header:=xlYes, _
    Order1:=xlAscending
   
End Sub
 

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.
Try the following for your sort:
VBA Code:
Range("A2:G" & Range("A" & Rows.Count).End(xlUp).Row).Sort key1:=Range("D2"), order1:=xlAscending, DataOption1:=xlSortTextAsNumbers, _
      key2:=Range("B2"), order2:=xlAscending, DataOption2:=xlSortNormal, Header:=xlYes
 
Upvote 0
Try the following for your sort:
VBA Code:
Range("A2:G" & Range("A" & Rows.Count).End(xlUp).Row).Sort key1:=Range("D2"), order1:=xlAscending, DataOption1:=xlSortTextAsNumbers, _
      key2:=Range("B2"), order2:=xlAscending, DataOption2:=xlSortNormal, Header:=xlYes
Thanks, I tried it but still get goofy results it didnt sort the box # correctly it seems:
1661805232307.png



Like i said this code seems to work just not really understanding why i have to add the 2nd range section for it to sort properly????:
VBA Code:
Sub MultiLevelSort()

Worksheets("User Inputs").Sort.SortFields.Clear

Range("A2:G" & Range("A" & Rows.Count).End(xlUp).Row).Sort Key1:=Range("D2"), Key2:=Range("B2"), Header:=xlYes, _
Order1:=xlAscending, Order2:=xlAscending, DataOption1:=xlSortTextAsNumbers
   
Range("A2:G" & Range("A" & Rows.Count).End(xlUp).Row).Sort Key1:=Range("B2"), Header:=xlYes, _
Order1:=xlAscending
   
End Sub
 
Upvote 0
The picture in your last post seems to be sorted fine.
 
Upvote 0
The picture in your last post seems to be sorted fine.
Yes it is by Job # but then it should sort the box # in ascending order as well. So while the job # sorts I'm not sure why its not sorting Box #'s together.

Something like this:

1661807279972.png
 
Upvote 0
It would be easier to assist you if you uploaded your results with XL2BB.

Which column do you definitely want completely sorted and which column do you want to be the secondary column which may or may not end up completely sorted?
 
Upvote 0
@keef2 Judging by your image in post 5 Key1 should be B2 and Key2 should be D2
 
Upvote 0
I planned on posting the different results from different sorts, but I don't feel like typing in all the data.
 
Upvote 0

Forum statistics

Threads
1,215,368
Messages
6,124,520
Members
449,169
Latest member
mm424

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