Stack multiple columns into two colums in pairs of two

navelgazer

New Member
Joined
Mar 9, 2012
Messages
14
<table><tbody><tr><td class="votecell">
</td> <td class="postcell"> I'm new to excel and VBA and I'm stuck at this problem. I have columns A through AKP (A:AKP) with data, and there is a varied number of rows for each column. If possible, I need a macro that will "stack" columns in pairs of two. For example, column "C" directly under column "A" and column "D" directly under column "B" and so on for all columns A:AKP.



This is an example of what my data looks like:


<code> COLUMN A COLUMN B COLUMN C COLUMN D
ROW 1 2598 F800 2599 F800
ROW 2 2598 K1300 2599 K1300
ROW 3 2598 S1000RR 2599 R900
ROW 4 2598 G650 2599 G650
ROW 5 2598 R1200 2599 K1600
ROW 6 2599 S1000
ROW 7 2599 HP2
ROW 8 2599 R1200
.
.
.
</code>
This is an example of what I need the output to look like:

<code> COLUMN A COLUMN B
ROW 1 2598 F800
ROW 2 2598 K1300
ROW 3 2598 S1000RR
ROW 4 2598 G650
ROW 5 2598 R1200
ROW 6 2599 S1000
ROW 7 2599 HP2
ROW 8 2599 R1200
ROW 9 2599 F800
ROW 10 2599 K1300
ROW 11 2599 R900
ROW 12 2599 G650
ROW 13 2599 K1600
.
.
.

Thank you in advance!:)
</code>

</td></tr></tbody></table>
 
Last edited:

Tom Urtis

MrExcel MVP
Joined
Feb 10, 2002
Messages
11,197
Try this on a copy of your worksheet:

Code:
Sub Test1()
Application.ScreenUpdating = False
Dim xColumn&, LastRow&, NextRow&
For xColumn = 3 To 977 Step 2
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
LastRow = Cells(Rows.Count, xColumn).End(xlUp).Row
Range(Cells(1, xColumn), Cells(LastRow, xColumn + 1)).Cut Cells(NextRow, 1)
Next xColumn
Application.ScreenUpdating = True
End Sub
 

repairman615

Well-known Member
Joined
Dec 21, 2009
Messages
1,885
Hello,




<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> TestMeOnACopy()<br>Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> Ws <SPAN style="color:#00007F">As</SPAN> Worksheet: <SPAN style="color:#00007F">Set</SPAN> Ws = ActiveSheet<br><SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> lastRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    j = 1<br>    <SPAN style="color:#00007F">For</SPAN> i = 3 <SPAN style="color:#00007F">To</SPAN> 978<br>        lastRow = Ws.Cells(1, i).End(xlDown).Offset(1, 0).Row<br>        <SPAN style="color:#00007F">If</SPAN> j = 3 <SPAN style="color:#00007F">Then</SPAN> j = 1<br>        Ws.Cells(1, i).Resize(lastRow, 1).Copy Destination:= _<br>                Cells(1, j).End(xlDown).Offset(1, 0)<br>        j = j + 1<br>    <SPAN style="color:#00007F">Next</SPAN> i<br><SPAN style="color:#007F00">'''ws.Range("C:AKP").Delete</SPAN><br><SPAN style="color:#00007F">Set</SPAN> Ws = <SPAN style="color:#00007F">Nothing</SPAN><br>Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Last edited:

Forum statistics

Threads
1,081,840
Messages
5,361,618
Members
400,642
Latest member
tekster23

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top