Excel macro - multiple rows to single row

Barnsley

New Member
Joined
Aug 23, 2017
Messages
1
Hello!

I wonder if someone could help me? I'm trying to combine multiple rows into a single row. My data currently is formatted:

E-mail,First Name,Last Name,Event,Ticket Name,Spaces
email1,John,Taylor,C,81,5
email2,Mary,Smith,A,81,2
email2,Mary,Smith,D,144,1
email2,Mary,Smith,I,81,1
email2,Mary,Smith,G,82,1
email1,John,Taylor,A,81,2

I'd like to be able to combine rows so that my data looks like this:

E-mail,First Name,Last Name,Event,Ticket Name,Spaces,Event,Ticket Name,Spaces,Event,Ticket Name,Spaces,Event,Ticket Name,Spaces
email1,John,Taylor,C,81,5,A,81,2
email2,Mary,Smith,A,81,2,D,144,1,I,81,1,G,82,1

I found a macro on an old techguy thread which just about does the job, only it keeps repeating the last name and doesn't reprint the header. I've tried to edit it but I know nothing of Visual Basic and can't figure out how it's working. Here's the code from that thread:

Code:
Sub test()
MainSheetRows = Range("B" & Rows.Count).End(xlUp).Row
For Each Cell In Range("B1:B" & MainSheetRows)
x = WorksheetFunction.CountIf(Range("New!B:B"), Cell)
If x = 0 Then
y = WorksheetFunction.CountA(Rows(Cell.Row))
Cell.Offset(, -1).Resize(, y).Copy Sheets("New").Range("A" & Rows.Count).End(xlUp).Offset(1)
Else
y = WorksheetFunction.CountA(Rows(Cell.Row)) - 2
z1 = Application.Match(Cell, Range("New!B:B"), 0)
z2 = WorksheetFunction.CountA(Sheets("New").Rows(z1))
Cell.Offset(, 1).Resize(, y).Copy Sheets("New").Cells(z1, z2 + 1)
End If
Next Cell
End Sub

Is anyone able to help?

Many thanks!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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