VBA for transposing a "6 column undefined row table" to only 1 column

Jason56

New Member
Joined
Mar 7, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I have a 6 column table with a variable rows (B2 to G? of sheet 2).

113637383945
21415212946
71618434548
161727404145
............

I want to transpose all cells into only a single column (for example column A of sheet 4)

11
36
37
38
39
45
2
14
15
21
..
..
..
..

Could you please help me to have a VBA macro for that?

Thanks.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try:

VBA Code:
Dim lr As Long, arrData, arrRes(), i As Long, j As Long, a As Long

Sheets("Sheet4").Columns(1).ClearContents
With Sheets("Sheet2")
    lr = .Range("B" & .Rows.Count).End(xlUp).Row
    If lr > 1 Then
        arr = .Range("B2:G" & lr)
        ReDim arrRes(1 To 6 * (lr - 1))
        For i = LBound(arr, 1) To UBound(arr, 1)
            For j = LBound(arr, 2) To UBound(arr, 2)
                a = a + 1
                arrRes(a) = arr(i, j)
            Next
        Next
        Sheets("Sheet4").Range("A1").Resize(UBound(arrRes)) = Application.Transpose(arrRes)
    End If
End With
 
Upvote 0
Solution
Can also do it with dynamic array formulas.

NumberGame.xlsm
ABCDEFGH
111363738394511
22141521294636
37161843454837
416172740414538
539
645
72
814
915
1021
1129
1246
137
1416
1518
1643
1745
1848
1916
2017
2127
2240
2341
2445
Sheet9
Cell Formulas
RangeFormula
H1:H24H1=LET(tbl,A1:F4,c,COLUMNS(tbl),seq,SEQUENCE(c*ROWS(tbl),,0),INDEX(tbl,seq/c+1,MOD(seq,c)+1))
Dynamic array formulas.
 
Upvote 0
Try:

VBA Code:
Dim lr As Long, arrData, arrRes(), i As Long, j As Long, a As Long

Sheets("Sheet4").Columns(1).ClearContents
With Sheets("Sheet2")
    lr = .Range("B" & .Rows.Count).End(xlUp).Row
    If lr > 1 Then
        arr = .Range("B2:G" & lr)
        ReDim arrRes(1 To 6 * (lr - 1))
        For i = LBound(arr, 1) To UBound(arr, 1)
            For j = LBound(arr, 2) To UBound(arr, 2)
                a = a + 1
                arrRes(a) = arr(i, j)
            Next
        Next
        Sheets("Sheet4").Range("A1").Resize(UBound(arrRes)) = Application.Transpose(arrRes)
    End If
End With
Thank you soo much
 
Upvote 0
Can also do it with dynamic array formulas.

NumberGame.xlsm
ABCDEFGH
111363738394511
22141521294636
37161843454837
416172740414538
539
645
72
814
915
1021
1129
1246
137
1416
1518
1643
1745
1848
1916
2017
2127
2240
2341
2445
Sheet9
Cell Formulas
RangeFormula
H1:H24H1=LET(tbl,A1:F4,c,COLUMNS(tbl),seq,SEQUENCE(c*ROWS(tbl),,0),INDEX(tbl,seq/c+1,MOD(seq,c)+1))
Dynamic array formulas.
Thank you
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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