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
 
It XL2BB didn't work on that sheet for some reason.... Would just lock up excel every time I tried even tho it works great on other workbooks.

To be clear i want it to sort by job # in ascending order. Then by Box number in ascending order. Like i said my code works i just don't understand why i needed the 2nd range to get it to work how i needed it from the above screen shot.

That said only thing i can think of is i used the LEN function in an equation so I sorted the range by DataOption1:=xlSortTextAsNumbers then with X1sortnormal.
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hope this is clearer, ultimately I want it sorted by box #. However before that i want all job #'s for those box #'s to be sorted and not the 33XX between the 22xxxxxxx numbers. Basically all the 33's and all the 22s should be grouped in ascending order within each box #.
 
Upvote 0
Did you swap the keys around as per post number 7?
 
Upvote 0
Deleted as looked at the wrong table, question in my last post still stands
 
Upvote 0
It XL2BB didn't work on that sheet for some reason.... Would just lock up excel every time I tried even tho it works great on other workbooks.
You don't need to to try and capture the entire sheet with XL2BB. You could just do like 50 or 100 rows.

XL2BB has a limit of like 3000 cells if I recall correctly.
 
Upvote 0
Did you swap the keys around as per post number 7?
yes that sorts by box # then job #. Basically creates a job # ascending list that doesn't group all box's together. Again tried to switch D2 and B2: Same result (see screen shot).

Again this is the code that does what i want it to here (just trying to understand if there is better approach or understand why i need to have redudant code to get it to sort how i wanted by job# and grouped with each box # together.:

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


Here was the code i tried to see if that would fix the sort without having to do it again.
VBA Code:
Sub MultiLevelSort()

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

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

End Sub


1661819627683.png
 
Upvote 0
You don't need to to try and capture the entire sheet with XL2BB. You could just do like 50 or 100 rows.

XL2BB has a limit of like 3000 cells if I recall correctly.
I know still doesnt work...
 
Upvote 0
If you say so.

Perhaps you could provide a link to the workbook with any 'Sensitive' info changed?
 
Upvote 0
So i think this is solved. Sorry that I couldn't get XLB22 to work on this workbook for some reason even tried to reinstall etc.

Long story short. I used LEN function in the equation to get the job #, thus it was showing some data as text while mixed with values. So i added the Value function to the equation:

New formula in column D = =IF(A13471="","",VALUE(RIGHT(A13471,LEN(A13471)-FIND("#",A13471))))
Previous formula in column D = =IF(A13247="","",RIGHT(A13247,LEN(A13247)-FIND("#",A13247)))

New VBA code i used:
VBA Code:
Sub MultiLevelSort()
    
    Dim sht As Worksheet: Set sht = ThisWorkbook.Sheets("User Inputs")
    bot_row = sht.UsedRange.Rows.Count
    sht.Sort.SortFields.Clear

    sht.Range("A2:G" & bot_row).Sort Key1:=sht.Range("B2"), Order1:=xlAscending, Key2:=sht.Range("D2"),  Order2:=xlAscending, Header:=xlYes

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,064
Messages
6,122,939
Members
449,094
Latest member
teemeren

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