VBA Transpose Multiple Rows

jardenp

Active Member
Joined
May 12, 2009
Messages
373
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
I'm not very familiar with looping, so can someone help me transpose multiple rows?

I need this:
[TABLE="class: grid, width: 400"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Prop1[/TD]
[TD]Prop2[/TD]
[TD]Prop3[/TD]
[TD]Prop4[/TD]
[/TR]
[TR]
[TD]Pauline[/TD]
[TD]Papaya[/TD]
[TD]Pear[/TD]
[TD]Peach[/TD]
[TD]Plum[/TD]
[/TR]
[TR]
[TD]Kael[/TD]
[TD]Koala[/TD]
[TD]Kiwi[/TD]
[TD]Kudu[/TD]
[TD]Kakapo[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

To become this:
[TABLE="class: grid, width: 400"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Description[/TD]
[TD]Item[/TD]
[/TR]
[TR]
[TD]Pauline[/TD]
[TD]Prop1[/TD]
[TD]Papaya[/TD]
[/TR]
[TR]
[TD]Pauline[/TD]
[TD]Prop2[/TD]
[TD]Pear[/TD]
[/TR]
[TR]
[TD]Pauline[/TD]
[TD]Prop3[/TD]
[TD]Peach[/TD]
[/TR]
[TR]
[TD]Pauline[/TD]
[TD]Prop4[/TD]
[TD]Plum[/TD]
[/TR]
[TR]
[TD]Kael[/TD]
[TD]Prop1[/TD]
[TD]Koala[/TD]
[/TR]
[TR]
[TD]Kael[/TD]
[TD]Prop2[/TD]
[TD]Kiwi[/TD]
[/TR]
[TR]
[TD]Kael[/TD]
[TD]Prop3[/TD]
[TD]Kudu[/TD]
[/TR]
[TR]
[TD]Kael[/TD]
[TD]Prop4[/TD]
[TD]Kakapo[/TD]
[/TR]
</tbody>[/TABLE]

This can be done on the same sheet, leaving the original. For instance maybe the first cell of the created table is in G1.

Any help would be appreciated!

Thanks,

Josh in IN
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Forget what I just said, I'll post something that works if no one else does first.
 
Last edited:
Upvote 0
Definitely check it out (works great on numerical data) but you have text, so a VBA solution (or formulas) is needed instead.
 
Upvote 0
Try this. You will need to make adjustments to your sheet names more than likely.

Code:
Sub xPose()
Dim LR      As Long
Dim LC      As Long
Dim x       As Long
Dim y       As Long
Dim wsIn    As Worksheet
Dim wsOut   As Worksheet

Set wsIn = Sheets("Sheet1")
Set wsOut = Sheets("Sheet2")
LR = wsIn.Range("A" & Rows.Count).End(xlUp).Row
LC = wsIn.Range("XFD1").End(xlToLeft).Column

For x = 2 To LR
    For y = 2 To LC
        wsOut.Range("A" & Rows.Count).End(xlUp).Offset(1).Value = wsIn.Cells(x, 1)
        wsOut.Range("B" & Rows.Count).End(xlUp).Offset(1).Value = wsIn.Cells(1, y)
        wsOut.Range("C" & Rows.Count).End(xlUp).Offset(1).Value = wsIn.Cells(x, y)
    Next y
Next x

End Sub
 
Upvote 0
This works too:


Excel 2010
ABCDEFGH
1NameProp1Prop2Prop3Prop4Prop5Prop6Prop7
2PaulinePapayaPearPeachPlumPinkParisPan
3KaelKoalaKiwiKuduKakapoKentuckyKatmanduKar
4BlueBlackBarleyBeigeBrownBoltBrickBass
5TealTurquoiseTanTarTelephoneTryTankTin
Sheet4 (4)


Code:
Sub transpdata()
Dim LR%, LC%, i%, x%
LR = Cells(Rows.Count, 1).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column
Cells(1, LC + 2).Resize(, 3).Value = Array("Name", "Description", "Item")
x = 0
For i = 2 To LR
Cells(i + x, LC + 2).Resize(LC - 1).Value = Cells(i, 1).Value
Cells(i + x, LC + 3).Resize(LC - 1).Value = Application.Transpose(Cells(1, 2).Resize(, LC - 1).Value)
Cells(i + x, LC + 4).Resize(LC - 1).Value = Application.Transpose(Cells(i, 2).Resize(, LC - 1).Value)
x = x + LC - 2
Next
End Sub


Excel 2010
ABCDEFGHIJKL
1NameProp1Prop2Prop3Prop4Prop5Prop6Prop7NameDescriptionItem
2PaulinePapayaPearPeachPlumPinkParisPanPaulineProp1Papaya
3KaelKoalaKiwiKuduKakapoKentuckyKatmanduKarPaulineProp2Pear
4BlueBlackBarleyBeigeBrownBoltBrickBassPaulineProp3Peach
5TealTurquoiseTanTarTelephoneTryTankTinPaulineProp4Plum
6PaulineProp5Pink
7PaulineProp6Paris
8PaulineProp7Pan
9KaelProp1Koala
10KaelProp2Kiwi
11KaelProp3Kudu
12KaelProp4Kakapo
13KaelProp5Kentucky
14KaelProp6Katmandu
15KaelProp7Kar
16BlueProp1Black
17BlueProp2Barley
18BlueProp3Beige
19BlueProp4Brown
20BlueProp5Bolt
21BlueProp6Brick
22BlueProp7Bass
23TealProp1Turquoise
24TealProp2Tan
25TealProp3Tar
26TealProp4Telephone
27TealProp5Try
28TealProp6Tank
29TealProp7Tin
Sheet4 (4)
 
Upvote 0
Wow! lrobbo314 and sheetspread, thank you so much. I actually have one application where lrobbo314's two-sheet solution works best and another where sheetspread's variable scale solution does. The calendar says June, but it sure feels like Christmas! Thank you so much for such elegant solutions!

Josh in IN
 
Upvote 0
Cool! Glad that you got it all squared away. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

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