Macro to help me transpose this information?

suri tamna

New Member
Joined
Oct 8, 2019
Messages
1
I searched for a similar situation to mine and didn't see one (apologies if one exists and I missed it!)

I have to format a long list of managers and their employees for mass emails, and the lists I get from our database are:


[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Manager 1[/TD]
[TD]Employee A[/TD]
[/TR]
[TR]
[TD]Manager 2[/TD]
[TD]Employee B[/TD]
[/TR]
[TR]
[TD]Manager 2[/TD]
[TD]Employee C[/TD]
[/TR]
[TR]
[TD]Manager 2[/TD]
[TD]Employee D[/TD]
[/TR]
[TR]
[TD]Manager 3[/TD]
[TD]Employee E[/TD]
[/TR]
[TR]
[TD]Manager 3[/TD]
[TD]Employee F[/TD]
[/TR]
[TR]
[TD]Manager 4[/TD]
[TD]Employee G[/TD]
[/TR]
[TR]
[TD]Manager 4[/TD]
[TD]Employee H[/TD]
[/TR]
</tbody>[/TABLE]

But I need the data to be formatted like this:

[TABLE="class: grid, width: 700"]
<tbody>[TR]
[TD]Manager 1[/TD]
[TD]Employee A[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Manager 2[/TD]
[TD]Employee B[/TD]
[TD]Employee C[/TD]
[TD]Employee D[/TD]
[/TR]
[TR]
[TD]Manager 3[/TD]
[TD]Employee E[/TD]
[TD]Employee F[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Manager 4[/TD]
[TD]Employee G[/TD]
[TD]Employee H[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


So, I'd like to sort all employees who report to the same manager so that they appear on the row next to that manager rather than in a column with the manager's name listed as many times as there are employees reporting to that manager. I sometimes have situations where there are 20 people reporting to a manager, and I have thousands of records to transpose, so doing this manually is a real bummer.

Thank you in advance to any solutions, and let me know if I can clarify anything.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Welcome to the MrExcel board!

If the list is in columns A:B starting at row 2 and results can go in columns D, E, F, ... then give this a try in a copy of your workbook.

Rich (BB code):
Sub Rearrange()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    d(a(i, 1)) = d(a(i, 1)) & "," & a(i, 2)
  Next i
  With Range("D2:E2").Resize(d.Count)
    .Value = Application.Transpose(Array(d.Keys, d.Items))
    .Columns(2).TextToColumns DataType:=xlDelimited, Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 9))
    .CurrentRegion.Columns.AutoFit
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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