Transposing Data

Jerry138889

New Member
Joined
May 23, 2013
Messages
28
Hi

I haven't been able to figure out how to phrase this to utilise the search here or on Google to find the answer, although I'm sure it is fairly simple.

I have data in the following format:

[TABLE="width: 128"]
<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl64, width: 64"]Company[/TD]
[TD="class: xl64, width: 64"] Rate[/TD]
[/TR]
[TR]
[TD="class: xl63"]AAA[/TD]
[TD="class: xl63"]Rate 1[/TD]
[/TR]
[TR]
[TD="class: xl63"]AAA[/TD]
[TD="class: xl63"]Rate 2[/TD]
[/TR]
[TR]
[TD="class: xl63"]AAA[/TD]
[TD="class: xl63"]Rate 3[/TD]
[/TR]
[TR]
[TD="class: xl63"]BBB [/TD]
[TD="class: xl63"]Rate 1[/TD]
[/TR]
[TR]
[TD="class: xl63"]BBB [/TD]
[TD="class: xl63"]Rate 2[/TD]
[/TR]
[TR]
[TD="class: xl63"]CCC[/TD]
[TD="class: xl63"]Rate 1[/TD]
[/TR]
[TR]
[TD="class: xl63"]CCC[/TD]
[TD="class: xl63"]Rate 2[/TD]
[/TR]
[TR]
[TD="class: xl63"]CCC[/TD]
[TD="class: xl63"]Rate 3[/TD]
[/TR]
[TR]
[TD="class: xl63"]CCC[/TD]
[TD="class: xl63"]Rate 4[/TD]
[/TR]
</tbody>[/TABLE]

and I want it to be displayed in the following format

[TABLE="width: 320"]
<colgroup><col width="64" span="5" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl66, width: 64"]Company[/TD]
[TD="class: xl66, width: 64"] Rate[/TD]
[TD="class: xl66, width: 64"]Rate[/TD]
[TD="class: xl66, width: 64"]Rate[/TD]
[TD="class: xl65, width: 64"] [/TD]
[/TR]
[TR]
[TD="class: xl65"]AAA[/TD]
[TD="class: xl65"]Rate 1[/TD]
[TD="class: xl65"]Rate 2[/TD]
[TD="class: xl65"]Rate 3
[/TD]
[TD="class: xl65"] [/TD]
[/TR]
[TR]
[TD="class: xl65"]
BBB

[/TD]
[TD="class: xl65"]Rate 1[/TD]
[TD="class: xl65"]Rate 2[/TD]
[TD="class: xl65"] [/TD]
[TD="class: xl65"] [/TD]
[/TR]
[TR]
[TD="class: xl65"]CCC[/TD]
[TD="class: xl65"]Rate 1[/TD]
[TD="class: xl65"]Rate 2[/TD]
[TD="class: xl65"]Rate 3[/TD]
[TD="class: xl65"]
Rate 4

[/TD]
[/TR]
</tbody>[/TABLE]


Sort of like an advanced transpose function. A pivot doesn't seem to do it correctly.

Thanks for the help
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
How about
Code:
Sub ConsolidateTranspose()

   Dim Cl As Range
   Dim Itm As Variant
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then
            .Add Cl.Value, Cl.Offset(, 1).Value
         Else
            .Item(Cl.Value) = .Item(Cl.Value) & "|" & Cl.Offset(, 1).Value
         End If
      Next Cl
      Range("A1").CurrentRegion.Offset(1).Clear
      Range("A2").Resize(.Count).Value = Application.Transpose(.keys)
      For Each Itm In .Items
         Range("B" & Rows.Count).End(xlUp).Offset(1).Resize(, UBound(Split(Itm, "|")) + 1).Value = Split(Itm, "|")
      Next Itm
   End With
End Sub
 
Upvote 0
How about
Code:
Sub ConsolidateTranspose()

   Dim Cl As Range
   Dim Itm As Variant
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then
            .Add Cl.Value, Cl.Offset(, 1).Value
         Else
            .Item(Cl.Value) = .Item(Cl.Value) & "|" & Cl.Offset(, 1).Value
         End If
      Next Cl
      Range("A1").CurrentRegion.Offset(1).Clear
      Range("A2").Resize(.Count).Value = Application.Transpose(.keys)
      For Each Itm In .Items
         Range("B" & Rows.Count).End(xlUp).Offset(1).Resize(, UBound(Split(Itm, "|")) + 1).Value = Split(Itm, "|")
      Next Itm
   End With
End Sub

Fluff, that's perfect. I thought there may have been a way to do it outside of VBA but your code works wonderfully.
 
Upvote 0
Glad to help & thanks for the feedback.

There probably is a way to do it using formulae, but I don't know how.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
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