Stacking Columns in One Excel Sheet

Dag2

New Member
Joined
Jul 3, 2023
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi,

I want to stack columns B to F of the following Excel MiniSheet below column A:

Keywords_Search_Klemens.xlsm
ABCDEF
1aa1bb1cc1dd1ee1ff1
2aa2bb2cc2ee2ff2
3aa3bb3cc3ee3ff3
4aa4cc4ee4ff4
5aa5cc5ee5ff5
6aa6cc6ee6ff6
7aa7ee7ff7
8aa8ff8
9aa9ff9
10aa10ff10
11aa11
12aa12
13aa13
Sheet1


This is only a small fraction of the Data. In the real dataset I would need it to stack columns B to ABP of varying length (column A is actually the longest with a range from A1 to A89) under column A. I saw someone from a different thread perform a LET-function, but I couldn't get it to work.

Help is very much appreciated!

Best regards and thank you in advance!
Klemens
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Maybe:
Book1
ABCDEF
1aa1bb1cc1dd1ee1ff1
2aa2bb2cc2ee2ff2
3aa3bb3cc3ee3ff3
4aa4cc4ee4ff4
5aa5cc5ee5ff5
6aa6cc6ee6ff6
7aa7ee7ff7
8aa8ff8
9aa9ff9
10aa10ff10
11aa11
12aa12
13aa13
14bb1
15bb2
16bb3
17cc1
18cc2
19cc3
20cc4
21cc5
22cc6
23dd1
24ee1
25ee2
26ee3
27ee4
28ee5
29ee6
30ee7
31ff1
32ff2
33ff3
34ff4
35ff5
36ff6
37ff7
38ff8
39ff9
40ff10
Sheet1
Cell Formulas
RangeFormula
A14:A40A14=TOCOL(TRANSPOSE(B1:F10),1)
Dynamic array formulas.
 
Upvote 1
Solution
Another (similar) option
Fluff.xlsm
ABCDEF
1aa1bb1cc1dd1ee1ff1
2aa2bb2cc2ee2ff2
3aa3bb3cc3ee3ff3
4aa4cc4ee4ff4
5aa5cc5ee5ff5
6aa6cc6ee6ff6
7aa7ee7ff7
8aa8ff8
9aa9ff9
10aa10ff10
11aa11
12aa12
13aa13
14bb1
15bb2
16bb3
17cc1
18cc2
19cc3
20cc4
21cc5
22cc6
23dd1
24ee1
25ee2
26ee3
27ee4
28ee5
29ee6
30ee7
31ff1
32ff2
33ff3
34ff4
35ff5
36ff6
37ff7
38ff8
39ff9
40ff10
Master
Cell Formulas
RangeFormula
A14:A40A14=TOCOL(B1:F13,1,1)
Dynamic array formulas.
 
Upvote 1
Thank you so much guys!
In the end I stumbled across this solution, which also worked great for me, so I thought I share it:

VBA Code:
Sub Stack_cols()
 
    On Error GoTo Stack_cols_Error
     
    Dim lNoofRows As Long, lNoofCols As Long
    Dim lLoopCounter As Long, lCountRows As Long
    Dim sNewShtName As String
    Dim shtOrg As Worksheet, shtNew As Worksheet
     
    'Turn off the screen update to make macro run faster
    Application.ScreenUpdating = False
    'Ask for a new sheet name, if not provided use newsht
    sNewShtName = InputBox("Enter the new worksheet name", "Enter name", "newsht")
    'Set a sheet variable for the sheet where the data resides
    Set shtOrg = ActiveSheet
    'Add a new worksheet, rename it and set it to a variable
    If Not SheetExists(sNewShtName) Then
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sNewShtName
        Set shtNew = Worksheets(sNewShtName)
    Else
        MsgBox "Worksheet name exists. Try again", vbInformation, "Sheet Exists"
        Exit Sub
    End If
     
    With shtOrg
        'Get the last column number
        'Replace .Range("IV1") with .Range("XFD1") for Excel 2007
        lNoofCols = .Range("XFD1").End(xlToLeft).Column
        'Start a loop to copy and paste data from the first column to the last column
        For lLoopCounter = 1 To lNoofCols
        'Count the number of rows in the looping column
            'Replace .Cells(65536, lLoopCounter) with .Cells(1048576, lLoopCounter) for Excel 2007
            lNoofRows = .Cells(1048576, lLoopCounter).End(xlUp).Row
            .Range(.Cells(1, lLoopCounter), .Cells(lNoofRows, lLoopCounter)).Copy Destination:=shtNew.Range(shtNew.Cells(lCountRows + 1, 1), shtNew.Cells(lCountRows + lNoofRows, 1))
            'count the number of rows in the new worksheet
            lCountRows = lCountRows + lNoofRows
        Next lLoopCounter
    End With
     
    On Error GoTo 0
SmoothExit_Stack_cols:
        Application.ScreenUpdating = True
        Exit Sub
     
Stack_cols_Error:
' if this gives you an error, make sure that there an ampersand (&) sign, rather than the &amp literal string. the code formatting is an issue
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Sub:Stack_cols"
    Resume SmoothExit_Stack_cols
End Sub
'Check if a worksheet exists or not
Public Function SheetExists(sShtName As String) As Boolean
    On Error Resume Next
     
    Dim wsSheet As Worksheet, bResult As Boolean
    bResult = False
    Set wsSheet = Sheets(sShtName)
     
    On Error GoTo 0
    If Not wsSheet Is Nothing Then
        bResult = True
    End If
    SheetExists = bResult
End Function
 
Upvote 0
an alternative is with Power Query to unpivot your data

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(Source, {}, "Attribute", "Value")
in
    #"Unpivoted Columns"
 
Upvote 1
Using @Fluff formula and the evaluate function you could make the VBA to do something similar:
VBA Code:
Sub test()
    Dim rng As Range, nWS As Worksheet
    
    Set rng = Application.InputBox("Select range to transpose", Type:=8)
    Set nWS = Sheets.Add(, Sheets(Sheets.Count))
    nWS.Range("A1").Resize(Application.CountA(rng)) = Evaluate("TOCOL(" & rng.Address(, , , True) & ",1,1)")
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,951
Members
449,095
Latest member
nmaske

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