stack multiple columns

Urraco

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

I have a lot of columns, around 400 and I'm trying to stack this columns on top of each other, making single column.
In this example I would like to stack columns 3,4,5, 6 into a single column..and columns 1 and 2 to be repeated as many times as needed.
Is there any solution to do that instead of doing it manually?
Thank you very much


this is example

Book1.xlsx
ABCDEF
1123456
2A1B1161116
3A2B2271217
4A3B3381318
5A4B4491419
6A5B55101520
Sheet1


and the result

Book1.xlsx
ABC
1123
2A1B11
3A2B22
4A3B33
5A4B44
6A5B55
7A1B16
8A2B27
9A3B38
10A4B49
11A5B510
12A1B111
13A2B212
14A3B313
15A4B414
16A5B515
17A1B116
18A2B217
19A3B318
20A4B419
21A5B520
Sheet1
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Assuming that row 1 is your header, and the data starts on row 2, try this:
VBA Code:
Sub MyStackColumns()

    Dim lr As Long
    Dim lc As Long
    Dim c As Long
    Dim nr As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row

'   Find last column in row 1 with data
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    
'   Exit if last column is less than 4 (nothing to do)
    If lc < 4 Then Exit Sub
        
'   Loop through all columns, staring with column D
    For c = 4 To lc
'       Find first available row to paste to
        nr = Cells(Rows.Count, "A").End(xlUp).Row + 1
'       Copy columns A and B
        Range("A2:B" & lr).Copy Cells(nr, "A")
'       Copy data to column C
        Range(Cells(2, c), Cells(lr, c)).Cut Range("C" & nr)
    Next c
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
Try:
VBA Code:
Sub StackCols()
    Application.ScreenUpdating = False
    Dim LastRow As Long, lCol As Long, x As Long
    lCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = 4 To lCol
        Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(LastRow - 1, 2).Value = Range("A2").Resize(LastRow - 1, 2).Value
        Cells(Rows.Count, "C").End(xlUp).Offset(1).Resize(LastRow - 1, 2).Value = Cells(2, x).Resize(LastRow - 1).Value
    Next x
    Range("D1").Resize(, lCol - 3).EntireColumn.Delete
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Perfect!
Thank you very much!


Assuming that row 1 is your header, and the data starts on row 2, try this:
VBA Code:
Sub MyStackColumns()

    Dim lr As Long
    Dim lc As Long
    Dim c As Long
    Dim nr As Long
   
    Application.ScreenUpdating = False
   
'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row

'   Find last column in row 1 with data
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
   
'   Exit if last column is less than 4 (nothing to do)
    If lc < 4 Then Exit Sub
       
'   Loop through all columns, staring with column D
    For c = 4 To lc
'       Find first available row to paste to
        nr = Cells(Rows.Count, "A").End(xlUp).Row + 1
'       Copy columns A and B
        Range("A2:B" & lr).Copy Cells(nr, "A")
'       Copy data to column C
        Range(Cells(2, c), Cells(lr, c)).Cut Range("C" & nr)
    Next c
   
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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