VBA Transpose rows to columns

Skovgaard

Board Regular
Joined
Oct 18, 2013
Messages
204
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I've been searching the forum, but haven't been able to find exactly what I need.

I need a macro that gives below result, hope anyone can help.

Data:
[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD]Jan[/TD]
[TD]Feb[/TD]
[TD]Mar[/TD]
[/TR]
[TR]
[TD]P1[/TD]
[TD]Q1[/TD]
[TD]75[/TD]
[TD]100[/TD]
[TD]125[/TD]
[/TR]
[TR]
[TD]P2[/TD]
[TD]Q2[/TD]
[TD]30[/TD]
[TD]40[/TD]
[TD]50[/TD]
[/TR]
[TR]
[TD]P3[/TD]
[TD]Q2[/TD]
[TD]10[/TD]
[TD]40[/TD]
[TD]70[/TD]
[/TR]
</tbody>[/TABLE]

Result:

[TABLE="width: 500"]
<tbody>[TR]
[TD]P1[/TD]
[TD]Q1[/TD]
[TD]Jan[/TD]
[TD]75[/TD]
[/TR]
[TR]
[TD]P1[/TD]
[TD]Q1[/TD]
[TD]Feb[/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD]P1[/TD]
[TD]Q1[/TD]
[TD]Mar[/TD]
[TD]125[/TD]
[/TR]
[TR]
[TD]P2[/TD]
[TD]Q2[/TD]
[TD]Jan[/TD]
[TD]30[/TD]
[/TR]
[TR]
[TD]P2[/TD]
[TD]Q2[/TD]
[TD]Feb[/TD]
[TD]40[/TD]
[/TR]
[TR]
[TD]P2[/TD]
[TD]Q2[/TD]
[TD]Mar[/TD]
[TD]50[/TD]
[/TR]
[TR]
[TD]P3[/TD]
[TD]Q2[/TD]
[TD]Jan[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]P3[/TD]
[TD]Q2[/TD]
[TD]Feb[/TD]
[TD]40[/TD]
[/TR]
[TR]
[TD]P3[/TD]
[TD]Q2[/TD]
[TD]Mar[/TD]
[TD]70[/TD]
[/TR]
</tbody>[/TABLE]
Regards
Skovgaard
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Use power query & paste the code in advance editor

let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Jan", Int64.Type}, {"Feb", Int64.Type}, {"Mar", Int64.Type}}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Column1", "Column2"}, "Attribute", "Value")
in
#"Unpivoted Columns"
 
Upvote 0
For data in A1:E4 as shown below, the following code produced the results in row 7 and below. Is that what you want?

Code:
Sub Rearrange()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  
  a = Range("A1").CurrentRegion.Value
  ReDim b(1 To UBound(a, 1) * (UBound(a, 2) - 2), 1 To 4)
  For i = 2 To UBound(a, 1)
    For j = 3 To UBound(a, 2)
      k = k + 1
      b(k, 1) = a(i, 1): b(k, 2) = a(i, 2): b(k, 3) = a(1, j): b(k, 4) = a(i, j)
    Next j
  Next i
  Range("A" & Rows.Count).End(xlUp).Offset(3).Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

Excel Workbook
ABCDEF
1JanFebMar
2P1Q175100125
3P2Q2304050
4P3Q2104070
5
6
7P1Q1Jan75
8P1Q1Feb100
9P1Q1Mar125
10P2Q2Jan30
11P2Q2Feb40
12P2Q2Mar50
13P3Q2Jan10
14P3Q2Feb40
15P3Q2Mar70
16
Rearrange
 
Upvote 0
hi, that's very useful macro. How about the vice versa? I have the multiple rows of data and want to transpose to the multiple columns. Would you please help? Thanks, Stan

A Col1 1
A Col2 2
A Col3 3
A Col4 4
B
Col1 1
B
Col2 2
B
Col3 3
B
Col4 5

want to transpose like below:
Col1 Col2 Col3 Col4
A 1 2 3 4
B 1 2 3 5

Thanks.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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