VBA Macro to transpose Data

ascool_asice

New Member
Joined
May 4, 2007
Messages
13
Hi,

I was wondering if someone could help me out with a macro to transform my data from Columns to Rows.

From

Help.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1Stock Item NameCategory1Category2Category3Category4Category5Category6Category7Category8Category9Category10Category11Category12Category13Category14Category15Category16Category17Category18Category19Category20Category21Category22Category23Category24Category25
2AAB-10BC20
3B12345678910111213141516171819202122232425
4CE
5DAB-10BC20CDEFGHIJ
6F
From


Output should look like
Help.xlsx
AB
1Stock Item NameCategory
2AAB-10
3ABC20
4B1
5B2
6B3
7B4
8B5
9B6
10B7
11B8
12B9
13B10
14B11
15B12
16B13
17B14
18B15
19B16
20B17
21B18
22B19
23B20
24B21
25B22
26B23
27B24
28B25
29CE
30DAB-10
31DBC20
32DCD
33DEF
34DGH
35DIJ
36F
Transposed
 
Try this:

VBA Code:
Sub Transpose_Data()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  a = Sheets("From").Range("A1").CurrentRegion
  ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 2)
  
  k = 1
  For i = 2 To UBound(a, 1)
    For j = 2 To UBound(a, 2)
      b(k, 1) = a(i, 1)
      If a(i, j) <> "" Then
        b(k, 2) = a(i, j)
        k = k + 1
      End If
    Next
  Next
  Sheets("Transposed").Range("A2").Resize(k, 2).Value = b
End Sub
 
Upvote 0
The data was a little large so took me a little time to realize that there seems to be some problem it's leaving out a few entries. See below the result

From
Tally-01 - Final.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1Stock Item NameCategory1Category2Category3Category4Category5Category6Category7Category8Category9Category10Category11Category12Category13Category14Category15Category16Category17Category18Category19Category20Category21Category22Category23Category24Category25
2AAB-10BC20
3B12345678910111213141516171819202122232425
4CE
5DAB-10BC20CDEFGHIJ
6F
7G
8H
9I
10J
11K
From


Transposed. (F,G,H,I & J are missing)

Tally-01 - Final.xlsx
AB
1Stock Item NameCategory
2AAB-10
3ABC20
4B1
5B2
6B3
7B4
8B5
9B6
10B7
11B8
12B9
13B10
14B11
15B12
16B13
17B14
18B15
19B16
20B17
21B18
22B19
23B20
24B21
25B22
26B23
27B24
28B25
29CE
30DAB-10
31DBC20
32DCD
33DEF
34DGH
35DIJ
36K
Transposed
 
Upvote 0
Try this:

VBA Code:
Sub Transpose_Data()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  Dim bln As Boolean
  
  a = Sheets("From").Range("A1").CurrentRegion
  ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 2)
  
  k = 1
  For i = 2 To UBound(a, 1)
    bln = False
    For j = 2 To UBound(a, 2)
      b(k, 1) = a(i, 1)
      If a(i, j) <> "" Then
        b(k, 2) = a(i, j)
        k = k + 1
        bln = True
      End If
    Next
    If bln = False Then k = k + 1
  Next
  Sheets("Transposed").Range("A2").Resize(k, 2).Value = b
End Sub
 
Upvote 0

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