VBA Macro to Unstack Data

fouraces

New Member
Joined
May 8, 2017
Messages
8
I have data that has been stacked prior to my receiving it. There are multiple rows of values for a single unifying ID. I'd like to convert those multiple rows into a single row by adding columns and pulling in a specified range of data. So my initial data looks like:
IDNameSubjectDateCode
1BobGolf1-Marxyz
2AdamCards2-Marxcv
2JoeCards5-Marxch
3WillTravel6-Marvvv
3BillTravel1-Marcrt
3ThrillTravel2-Marabt
4NapoleonGrapeshot3-Marxso

<tbody>
</tbody>

And my output data should look like:
IDNameSubjectDateCodeColumn1Column2Column3Column4
1BobGolf1-Marxyz
2AdamCards2-MarxcvJoeCards
3WillTravel6-MarvvvBillTravelThrillTravel
4NapoleonGrapeshot3-Marxso

<tbody>
</tbody>

Ideally, I'd like to see the macro two ways- in this case, it's pulling from a range of columns that are together (B:C). However, I'd also like to see it if the columns needed were not consecutive (B and E for example).
Thanks for any help, appreciate it!!
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I'm dashing to go out so I can't write you any VBA at the moment but... can this be done with a pivot table?
 
Last edited:
Upvote 0
I'm dashing to go out so I can't write you any VBA at the moment but... can this be done with a pivot table?

Thanks for your reply. A standard pivot table cannot display text values, which my columns typically are. In my research, I saw that Get and Transform (aka PowerQuery) might be able to make a semblance of this. However, for a data set of any size (even 100 rows), I couldn't get this result- not even close really. If you have a method to do it manually as a process, that would certainly be welcome as well. I can get there using IF formulas, but in the very possible scenario that there may be 20+ rows tied to the same ID, and I need to pull 7 columns for each row, it takes a fair bit of time to adjust the formulas in every new column. More importantly is avoiding the potential for manual error as well. Thanks!
 
Upvote 0
In case anyone else runs into this issue, wanted to post the code I ended up using (disclaimer: got some help to get it done)

Code:
[FONT=Calibri]Sub UnstackData()[/FONT][FONT=Calibri]  Dim wSht1 As Worksheet, wSht2 As Worksheet[/FONT]
[FONT=Calibri]  Set wSht1 = Sheets("Sheet1")[/FONT]
[FONT=Calibri]  Set wSht2 = Sheets("Sheet2")[/FONT]

[FONT=Calibri]  Dim r As Integer: r = 2[/FONT]
[FONT=Calibri]  Dim r2 As Integer: r2 = 1[/FONT]
[FONT=Calibri]  Dim r1 As Integer, c2 As Integer[/FONT]

[FONT=Calibri]  With wSht1[/FONT]
[FONT=Calibri]    wSht2.Rows(1).Value = .Rows(1).Value[/FONT]
[FONT=Calibri]    r1 = .Cells(.Rows.Count,"A").End(xlUp).Row[/FONT]

[FONT=Calibri]    Do While r <= r1[/FONT]
[FONT=Calibri]      c2 = 16 [/FONT]

[FONT=Calibri]      Do While .Cells(r, "A") =.Cells(r - 1, "A")[/FONT]
[FONT=Calibri]        wSht2.Cells(r2, c2).Value = .Cells(r,"B").Value[/FONT]
[FONT=Calibri]        wSht2.Cells(r2, c2 + 1).Value =.Cells(r, "C").Value[/FONT]
[FONT=Calibri]        c2 = c2 + 2[/FONT]
[FONT=Calibri]        r = r + 1[/FONT]
[FONT=Calibri]      Loop[/FONT]
[FONT=Calibri]      r2 = r2 + 1[/FONT]

[FONT=Calibri]      Do While .Cells(r, "A")<> .Cells(r - 1, "A")[/FONT]
[FONT=Calibri]        wSht2.Range("A" & r2& ":E" & r2).Value = .Range("A" & r &":E" & r).Value[/FONT]
[FONT=Calibri]        r = r + 1[/FONT]
[FONT=Calibri]        r2 = r2 + 1[/FONT]
[FONT=Calibri]      Loop[/FONT]
[FONT=Calibri]      r2 = r2 - 1[/FONT]

[FONT=Calibri]    Loop[/FONT]

[FONT=Calibri]  End With[/FONT]

[FONT=Calibri]End Sub[/FONT]
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,871
Members
449,097
Latest member
dbomb1414

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