Complex VBA Sorting

FrenchCelt

Board Regular
Joined
May 22, 2018
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to find a way to code VBA to sort a report by two categories, simple enough, but the added wrinkle is that there is an extra row that has to be included with the sort, and it throws everything off.

This is a snippet of the original data:

1609625745130.png


I'm trying to sort username alphabetically by shift, so the end result should look like this:

1609625828296.png


The shift order should be 10A, 10B, 12A, and 12B.

If I simply sort by those two categories, of course, this is what happens:

1609625918308.png


The data is put in an order that renders the report useless, so clearly I need to keep the user data together as a unit while sorting, but I'm at a loss to figure out how.

Does anyone have any suggestions?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Could you post your first table using XL2BB ?
So we can easily copy the data to test it.
 
Upvote 0
I'm using my own example:
The code:
VBA Code:
Sub a1157370a()

'https://www.mrexcel.com/board/threads/complex-vba-sorting.1157370/

Dim i As Long, n As Long
Dim h As String
Dim va

n = Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
va = Range("B2:B" & n)

    For i = 2 To UBound(va, 1)
        If va(i, 1) = "" Then va(i, 1) = va(i - 1, 1)
    Next
    
h = "E"  'helper column, change to suit

Range(h & "2").Resize(UBound(va, 1), 1) = va
Range("A2:" & h & n).Sort Key1:=Range(h & "2"), Order1:=xlAscending, Header:=xlNo
Range(h & "2").Resize(UBound(va, 1), 1).ClearContents

End Sub


Book1
ABC
1col1col2col3
2BMark
3Ford
4Preston
5vv
6AJoe
7Ford
8kk
9BFord
10Preston
11zz
12AHunter
13Joe
14nn
Sheet1


the result

Book1
ABC
1col1col2col3
2AJoe
3Ford
4kk
5AHunter
6Joe
7nn
8BMark
9Ford
10Preston
11vv
12BFord
13Preston
14zz
Sheet2


Note: col E is the helper column, change to suit:
h = "E" 'helper column, change to suit
 
Upvote 0
OK.
Did you tried the code in post #3 on your data?
You can change helper column from E to N in this line:
h = "E" 'helper column, change to suit
 
Upvote 0
The code writes some data to helper column then use it to sort the main data.
But it's only a temporary column, it will be deleted at the end of the code, so you won't see it unless you run the code from Debug menu (in VBE) and stop the code (using breakpoint) in this line:
Range(h & "2").Resize(UBound(va, 1), 1).ClearContents
 
Upvote 0
Thanks. I really like understanding what the code does so I can adapt the concept for future macros. That's pretty much how I've been learning. I'll let you know how it goes once I get some breathing room here at work.
 
Upvote 0
The code works as intended. Thank you very much!

This is what it looks like after running the macro:

1609698708890.png


The only thing I'll have to figure out is how to clean up the data in Columns A & B so it doesn't look so uneven (compare to how user dmcmillion looks as opposed to bcox and jabowman). The other columns look fine (I have code later on that adds a row between usernames, that much is deliberate).
 
Upvote 0
@FrenchCelt
Sorry, the code is actually flawed, because it doesn't sort the name just shift.
So try his one instead:
Note: you need 2 helper columns now, so I use h = "O"

VBA Code:
Sub a1157370b()

'https://www.mrexcel.com/board/threads/complex-vba-sorting.1157370/

Dim i As Long, n As Long
Dim h As String
Dim va

n = Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
va = Range("A2:B" & n)

    For i = 2 To UBound(va, 1)
        If va(i, 1) = "" Then va(i, 1) = va(i - 1, 1)
        If va(i, 2) = "" Then va(i, 2) = va(i - 1, 2)
    Next
    
h = "O"  'helper column, change to suit

    With Range(h & "2").Offset(, -1).Resize(UBound(va, 1), 2)
        .Value = va
        Range("A2:" & h & n).Sort Key1:=Range(h & "2").Offset(, -1), Order1:=xlAscending, Header:=xlNo
        Range("A2:" & h & n).Sort Key1:=Range(h & "2"), Order1:=xlAscending, Header:=xlNo
        .ClearContents
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,410
Messages
6,124,749
Members
449,186
Latest member
HBryant

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