Re-arranging and Sorting Data using a VBA Macro

clarkmarka

New Member
Joined
May 22, 2024
Messages
6
Office Version
  1. 2019
Platform
  1. MacOS
Hello Mr. Excel Forum,
I am constantly working with data that is presented in 4 columns. Please see below.
1716382259476.png

For my non profit, we create reports that are arranged such that the Memorial Name appears first, followed by the Constituents that contributed in honor of that person. However, the catch is that both fields (Memorial - Parent) and (Constituent - Child) are arranged in ABC order by Last Name. It should look like this.
1716382666054.png

I am hoping to have a macro that I can apply to data after downloading in this 4 column format that presents in this way, sorting each level of data by last name.
Any advice would be really appreciated. I've tried a number of macros I've found on this and other forums but nothing has worked.

Mark
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
@clarkmarka, welcome to the Forum.
Could you copy-paste the example here? so we don't need to retype it.
 
Upvote 0
Book1
ABCDEFG
1MemorialmLast_NameConstituentcLast_NameRandal Avery
2Pauline SmithSmithVicki NewtonNewtonTracy Lilly
3Pauline SmithSmithTracy LillyLillyMarcus Smith
4Randal AveryAveryTracy LillyLilly
5Randal AveryAveryMacus SmithSmithRichard Blake
6Richard BlakeBlakeMacus SmithSmithPaul Elias
7Richard BlakeBlakeTracy LillyLillyTracy Lilly
8Richard BlakeBlakeVicki NewtonNewtonVicki Newton
9Richard BlakeBlakePaul EliasEliasMarcus Smith
10
11Pauline Smith
12Tracy Lilly
13Vicki Newton
Sheet1
 
Upvote 0
I hope I pasted this correctly. I was intending for the results to display on a separate sheet, but wasn't sure how to show that.
 
Upvote 0
Try this:
VBA Code:
Sub clarkmarka()
Dim i As Long, j As Long, k As Long, n As Long
Dim va, vb, vx
n = Range("A" & Rows.Count).End(xlUp).Row
vx = Range("A2:D" & n)
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("B1"), Order:=xlAscending
        .SortFields.Add Key:=Range("A1"), Order:=xlAscending
        .SortFields.Add Key:=Range("D1"), Order:=xlAscending
        .SortFields.Add Key:=Range("C1"), Order:=xlAscending
        .SetRange Range("A1:D" & n)
        .Header = xlYes
        .Apply
    End With

'data start at row 2
va = Range("A2:C" & n + 1) 'n + 1 because I need to add 1 blank cell, otherwise if last cell is unique then
                           'the last i will get "Subscript out of range" on va(i + 1, 1)

ReDim vb(1 To UBound(va, 1) * 2, 1 To 2)

For i = 1 To UBound(va, 1) - 1
    j = i
    Do While va(i, 1) = va(i + 1, 1)
        i = i + 1
    Loop
    
    k = k + 1
    vb(k, 1) = va(j, 1)
    For x = j To i
        k = k + 1
        vb(k, 2) = va(x, 3)
    Next
Next
'put the result in F2
Range("F2").Resize(k, 2) = vb
Range("A2:D" & n) = vx
End Sub
 
Upvote 0
This looks great. What would I need to do if I wanted to make two changes to this.
1. Add a space before every new entry in the first column.
1716388852518.png

2. Present the data on a separate sheet. I imagine I would involve something with the range in the last step.
Screenshot 2024-05-22 at 9.41.10 AM.png


Thank you for all your advice so far. This is very helpful and I'm beginning to understand how it works.

Mark
 
Upvote 0
Try this one:
VBA Code:
Sub clarkmarka_2()
Dim i As Long, j As Long, k As Long, n As Long
Dim va, vb, vx
n = Range("A" & Rows.Count).End(xlUp).Row
vx = Range("A2:D" & n)
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("B1"), Order:=xlAscending
        .SortFields.Add Key:=Range("A1"), Order:=xlAscending
        .SortFields.Add Key:=Range("D1"), Order:=xlAscending
        .SortFields.Add Key:=Range("C1"), Order:=xlAscending
        .SetRange Range("A1:D" & n)
        .Header = xlYes
        .Apply
    End With


'data start at row 2
va = Range("A2:C" & n + 1) 'n + 1 because I need to add 1 blank cell, otherwise if last cell is unique then
                           'the last i will get "Subscript out of range" on va(i + 1, 1)

ReDim vb(1 To UBound(va, 1) * 2, 1 To 2)

For i = 1 To UBound(va, 1) - 1
    j = i
    Do While va(i, 1) = va(i + 1, 1)
        i = i + 1
    Loop
   
    k = k + 1
    vb(k, 1) = va(j, 1)
    For x = j To i
        k = k + 1
        vb(k, 2) = va(x, 3)
    Next
    k = k + 1
Next

Range("A2:D" & n) = vx

'put the result in sheet2
Sheets("Sheet2").Range("A2").Resize(k, 2) = vb

End Sub
 
Upvote 0
Solution
Hi - The spacing is working perfectly. However, when I try to put into Sheet 2, it simply doesn't run.
Do I need to add something custom to the code?

'put the result in sheet2
Sheets("Sheet2").Range("A2").Resize(k, 2) = vb

End Sub
 

Attachments

  • Screenshot 2024-05-22 at 10.08.11 AM.png
    Screenshot 2024-05-22 at 10.08.11 AM.png
    48.6 KB · Views: 5
  • Screenshot 2024-05-22 at 10.08.43 AM.png
    Screenshot 2024-05-22 at 10.08.43 AM.png
    81.4 KB · Views: 5
Upvote 0
The sheet's name should be "Sheet2" not "Sheet 2"
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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