Code to stack by row first rather than by column?

MeghanES

New Member
Joined
Aug 24, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello
I have the follow VBA code which stacks all of column A on top of column B and so on. What I need it to do now is stack all of row 1 on top of row 2 etc.

So rather than having:
A1
A2
A3
B1
B2
B3... etc.

It would instead be:
A1
B1
A2
B2
A3
B3... etc.

Any help would be appreciated, thank you inadvance!

VBA Code:
Sub Stack_cols()
 
    Dim LastRow     As Long, LastColumn As Long
    Dim Col         As Long, lCountRows As Long
    Dim sNewShtName As String
    Dim shtOrg      As Worksheet, shtNew As Worksheet
 
    On Error GoTo Stack_cols_Error:
 
    Do
        SendKeys "{END}"
        'Ask for a new sheet name
        sNewShtName = InputBox("Enter the New worksheet name", "Enter name", "newsht")
        'cancel pressed
        If StrPtr(sNewShtName) = 0 Then Exit Sub
      
        If Len(sNewShtName) > 0 Then
            'check if sheet exists
            If Not Evaluate("ISREF('" & sNewShtName & "'!A1)") Then
                'all ok
                Exit Do
            Else
                'inform user & try again
                MsgBox sNewShtName & Chr(10) & "Worksheet name exists. Try again", vbInformation, "Sheet Exists"
            End If
        End If
    Loop
 
    'Set a sheet variable for the sheet where the data resides
    Set shtOrg = ActiveSheet
 
    'Turn off the screen update to make macro run faster
    Application.ScreenUpdating = False
    'Add a new worksheet, rename it and set it to a variable
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sNewShtName
    Set shtNew = Worksheets(sNewShtName)
 
    With shtOrg
        'Get the last column number in row 1
        LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        'Start a loop to copy and paste data from the first column to the last column
        For Col = 1 To LastColumn
            'Count the number of rows in the looping column
            LastRow = .Cells(.Rows.Count, Col).End(xlUp).Row
            'copy only cells with constant values
            .Range(.Cells(1, Col), .Cells(LastRow, Col)).SpecialCells(xlCellTypeConstants).Copy _
            Destination:=shtNew.Range(shtNew.Cells(lCountRows + 1, 1), shtNew.Cells(lCountRows + LastRow, 1))
            'count of the number of non blank rows in column
            lCountRows = lCountRows + Application.CountA(.Columns(Col))
        Next Col
    End With
 
Stack_cols_Error:
    'report errors
    Application.ScreenUpdating = True
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
 
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hello
I have the follow VBA code which stacks all of column A on top of column B and so on. What I need it to do now is stack all of row 1 on top of row 2 etc.

So rather than having:
A1
A2
A3
B1
B2
B3... etc.

It would instead be:
A1
B1
A2
B2
A3
B3... etc.

Any help would be appreciated, thank you inadvance!

VBA Code:
Sub Stack_cols()
 
    Dim LastRow     As Long, LastColumn As Long
    Dim Col         As Long, lCountRows As Long
    Dim sNewShtName As String
    Dim shtOrg      As Worksheet, shtNew As Worksheet
 
    On Error GoTo Stack_cols_Error:
 
    Do
        SendKeys "{END}"
        'Ask for a new sheet name
        sNewShtName = InputBox("Enter the New worksheet name", "Enter name", "newsht")
        'cancel pressed
        If StrPtr(sNewShtName) = 0 Then Exit Sub
     
        If Len(sNewShtName) > 0 Then
            'check if sheet exists
            If Not Evaluate("ISREF('" & sNewShtName & "'!A1)") Then
                'all ok
                Exit Do
            Else
                'inform user & try again
                MsgBox sNewShtName & Chr(10) & "Worksheet name exists. Try again", vbInformation, "Sheet Exists"
            End If
        End If
    Loop
 
    'Set a sheet variable for the sheet where the data resides
    Set shtOrg = ActiveSheet
 
    'Turn off the screen update to make macro run faster
    Application.ScreenUpdating = False
    'Add a new worksheet, rename it and set it to a variable
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sNewShtName
    Set shtNew = Worksheets(sNewShtName)
 
    With shtOrg
        'Get the last column number in row 1
        LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        'Start a loop to copy and paste data from the first column to the last column
        For Col = 1 To LastColumn
            'Count the number of rows in the looping column
            LastRow = .Cells(.Rows.Count, Col).End(xlUp).Row
            'copy only cells with constant values
            .Range(.Cells(1, Col), .Cells(LastRow, Col)).SpecialCells(xlCellTypeConstants).Copy _
            Destination:=shtNew.Range(shtNew.Cells(lCountRows + 1, 1), shtNew.Cells(lCountRows + LastRow, 1))
            'count of the number of non blank rows in column
            lCountRows = lCountRows + Application.CountA(.Columns(Col))
        Next Col
    End With
 
Stack_cols_Error:
    'report errors
    Application.ScreenUpdating = True
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
 
End Sub
This is a link to my previoes thread where I got help with this code - How do I get this code to skip blank cells?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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