stack columns into multiple columns

Urraco

Board Regular
Joined
Apr 19, 2021
Messages
69
Office Version
  1. 2016
Platform
  1. Windows
Hi everybody!

I have a huge list, over 600.000 rows
I would like to distribute the columns x1 and x2 in groups of 500 rows in order, exactly as in the example below ( in the example I put only 5 rows to simplify)
is it possible with a power query or formula?
Thank yery much!


Book1
ABCDEFGHIJKLMNOPQRST
1x1x2a1a2b1b2c1c2d1d2e1e2f1f2g1g2h1h2i1i2
2119821198261987111992161983211997262002312007362012412017
3219832198371988121993171982221998272003322008372013422018
4319843198481989131994181983231999282004332009382014432019
5419854198591990141995191982242000292005342010392015442020
65198651986101991151996201983252001302006352011402016452021
761987462022512027562032612037662042712047762052812057862062
871988472023522028572033622038672043722048772053822058872063
981989482024532029582034632039682044732049782054832059882064
1091990492025542030592035642040692045742050792055842060892065
11101991502026552031602036652041702046752051802056852061902066
12111992912067962072
13121993922068
14131994932069
15141995942070
16151996952071
17161997
18171998
19181999
20192000
21202001
22212002
23222003
24232004
25242005
26252006
27262007
28272008
29282009
30292010
31302011
32312012
33322013
34332014
35342015
36352016
37362017
38372018
39382019
40392020
41402021
42412022
43422023
44432024
45442025
46452026
47462027
48472028
49482029
50492030
51502031
52512032
53522033
54532034
55542035
56552036
57562037
58572038
59582039
60592040
61602041
62612042
63622043
64632044
65642045
66652046
67662047
68672048
69682049
70692050
71702051
72712052
73722053
74732054
75742055
76752056
77762057
78772058
79782059
80792060
81802061
82812062
83822063
84832064
85842065
86852066
87862067
88872068
89882069
90892070
91902071
92912072
93922073
94932074
95942075
96952076
97962077
98972078
99982079
100992080
1011002081
1021012082
1031022083
1041032084
1051042085
1061052086
1071062087
1081072088
1091082089
1101092090
1111102091
1121112092
1131122093
1141132094
1151142095
1161152096
1171162097
1181172098
1191182099
1201192100
1211202101
Sheet1
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
how many groups do you want to go over (in both scenarios (your 500 and the 50 in the example? I ask this because the above is not groups of 50, it is groups of 45?
It just seems inconsistent if you were doing a proportional scenario in the mini workbook example.
 
Upvote 0
Perhaps the below will get you started; sorry the variables are not named well - i have been short of time:
VBA Code:
Sub test()
    Dim rng As Range, var As Variant
    Dim blocks As Long, rws As Long
    Dim StartCol As Long
    Dim a As Long, b As Long, c As Long, d As Long
    
    Set rng = Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row)
    var = rng.Value
    a = 1
    
    StartCol = 3 ' start column of results
    blocks = 9 ' how many blocks of 2 columns to use
    rws = 5 ' how many rows of data to include in each block
    
    Do
        For b = StartCol To (blocks * 2) + 1 Step 2
            For c = 2 + d To (rws + 1) + d
                If a > rng.Rows.Count Then Exit Do
                Cells(c, b) = var(a, 1)
                Cells(c, b + 1) = var(a, 2)
                a = a + 1
            Next c
        Next b
        d = d + (rws + 1)
    Loop
End Sub

The above could proably be streamlined also

It does not add headers to the blocks - not sure if the letters were essential or not?
 
Upvote 0
in this formula (from above), you will need to change the 45 to 500 (or whatever number you want in each section)
the first entry of the formula must be in cell C2, or you need to adjust it.

Excel Formula:
=INDEX( IF(ISODD(COLUMN(C2)), 1*($A$2:$A$600000),1*($B$2:$B$600000)), MOD((ROW(C2)-2),5)+1+ (INT((COLUMN(C2)-3)/2))*5+ INT((ROW(C2)-2)/5)*45,1)
 
Upvote 0
Tahks to all !!!

This macro almost does the job, the problem is that just adds an empty rows
can be solved?

Book1
ABCDEFGHIJKLMNOPQRST
1a1a2x1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17x18
2119821198261987111992161997212002262007312012362017412022
3219832198371988121993171998222003272008322013372018422023
4319843198481989131994181999232004282009332014382019432024
5419854198591990141995192000242005292010342015392020442025
65198651986101991151996202001252006302011352016402021452026
761987
871988462027512032562037612042662047712052762057812062862067
981989472028522033572038622043672048722053772058822063872068
1091990482029532034582039632044682049732054782059832064882069
11101991492030542035592040642045692050742055792060842065892070
12111992502031552036602041652046702051752056802061852066902071
13121993
141319949120729620771012082106208711120921162097121210212621071312112
151419959220739720781022083107208811220931172098122210312721081322113
161519969320749820791032084108208911320941182099123210412821091332114
171619979420759920801042085109209011420951192100124210512921101342115
1817199895207610020811052086110209111520961202101125210613021111352116
19181999
20192000136211714121221462127151213215621371612142166214717121521762157
21202001137211814221231472128152213315721381622143167214817221531772158
Sheet1


Perhaps the below will get you started; sorry the variables are not named well - i have been short of time:
VBA Code:
Sub test()
    Dim rng As Range, var As Variant
    Dim blocks As Long, rws As Long
    Dim StartCol As Long
    Dim a As Long, b As Long, c As Long, d As Long
  
    Set rng = Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row)
    var = rng.Value
    a = 1
  
    StartCol = 3 ' start column of results
    blocks = 9 ' how many blocks of 2 columns to use
    rws = 5 ' how many rows of data to include in each block
  
    Do
        For b = StartCol To (blocks * 2) + 1 Step 2
            For c = 2 + d To (rws + 1) + d
                If a > rng.Rows.Count Then Exit Do
                Cells(c, b) = var(a, 1)
                Cells(c, b + 1) = var(a, 2)
                a = a + 1
            Next c
        Next b
        d = d + (rws + 1)
    Loop
End Sub

The above could proably be streamlined also

It does not add headers to the blocks - not sure if the letters were essential or not?
 
Upvote 0
I left the blank rows in case you wanted headers, the below is with no blank rows:
VBA Code:
Sub test()
    Dim rng As Range, var As Variant
    Dim blocks As Long, rws As Long
    Dim StartCol As Long
    Dim a As Long, b As Long, c As Long, d As Long
    
    Set rng = Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row)
    var = rng.Value
    a = 1
    
    StartCol = 3 ' start column of results
    blocks = 9 ' how many blocks of 2 columns to use
    rws = 5 ' how many rows of data to include in each block
    
    Do
        For b = StartCol To (blocks * 2) + 1 Step 2
            For c = 1 + d To rws + d
                If a > rng.Rows.Count Then Exit Do
                Cells(c, b) = var(a, 1)
                Cells(c, b + 1) = var(a, 2)
                a = a + 1
            Next c
        Next b
        d = d + rws
    Loop
End Sub
 
Upvote 0
Solution
Thank you!
I get this error
Book3
ABCDEFG
21198211982#REF!#REF!#REF!
321983
431984
541985
651986
761987
871988
Sheet1
Cell Formulas
RangeFormula
C2:G2C2=INDEX( IF(ISODD(COLUMN(C2)), 1*($A$2:$A$600000),1*($B$2:$B$600000)), MOD((ROW(C2)-2),5)+1+ (INT((COLUMN(C2)-3)/2))*5+ INT((ROW(C2)-2)/5)*45,1)


@Urraco , did you consider the formula in post 4?
 
Upvote 0
@Urraco , I do not know why you would get that. I just copy and pasted your mini worksheet into a new workbook and I got this:
mr excel questions 13.xlsm
ABCDEFG
1
21198211982619870
321983
431984
541985
651986
761987
871988
Sheet12
Cell Formulas
RangeFormula
C2:G2C2=INDEX( IF(ISODD(COLUMN(C2)), 1*($A$2:$A$600000),1*($B$2:$B$600000)), MOD((ROW(C2)-2),5)+1+ (INT((COLUMN(C2)-3)/2))*5+ INT((ROW(C2)-2)/5)*45,1)
 
Upvote 0

Forum statistics

Threads
1,215,706
Messages
6,126,336
Members
449,310
Latest member
zztt388

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