VBA Transpose rows to columns

Skovgaard

Board Regular
Joined
Oct 18, 2013
Messages
195
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:
JanFebMar
P1Q175100125
P2Q2304050
P3Q2104070

<tbody>
</tbody>

Result:

P1Q1Jan75
P1Q1Feb100
P1Q1Mar125
P2Q2Jan30
P2Q2Feb40
P2Q2Mar50
P3Q2Jan10
P3Q2Feb40
P3Q2Mar70

<tbody>
</tbody>
Regards
Skovgaard
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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,213,487
Messages
6,113,938
Members
448,534
Latest member
benefuexx

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