Vertically stack columns

MossyPants

New Member
Joined
May 21, 2022
Messages
23
Office Version
  1. 365
Platform
  1. Windows
I'm looking for a way to vertically combine multiple columns of different lengths into a single master column. The columns' rows may be added or deleted from them in the future. When that happens, the master column should auto-update. I used to have a LET function that performed this task, but our company's Excel has been downgraded recently, and LET (or VSTACK) are no longer available.
Here is the worksheet. I would like every Set to be consolidated into column A.
Book1.xlsx
ABCD
1All SetsSetASetBSetC
2
3JB00 Fluorescent White CottonMz-1 Baby FoodEMPA-101 Carbon black/olive oil
4JB01 Carbon on CottonMz-2 TeaEMPA-104 Carbon black/olive oil on PolyCotton
5JB02 Protein on CottonMz-3 Spagetti SauceEMPA-106 Carbon black/mineral oil
6JB03 Sebum on CottonMz-4 MakeupEMPA-107 Carbon black/olive oil on Wool
7JB04 Water Soluble DirtMz-5 ClayEMPA-111 Blood
8JB05 Clay Type Dirt ClothMz-6 Carrot JuiceEMPA-112 Cocoa
9Mz-7 ACD BloodEMPA-114 Red wine
10Mz-8 Curry sauceEMPA-115 Immedial black
11Mz-9 Anchor ButterEMPA-116 Blood/milk/ink
12Mz-10 GrassEMPA-117 Blood/milk/ink on PolyCotton
13Mz-11 Chocolate DesertEMPA-118 Sebum/pigment
14Mz-12 Red WineEMPA-119 Sebum/pigment on PolyCotton
15Mz-13 Used Engine OilEMPA-120 Grease/quartz/iron oxide
16Mz-14 Animal Fat & DyeEMPA-122 Cotton soiled with Red Wine AGED
17Mz-15 Garden PeatEMPA-123 Cotton soiled Low Temp Washing
18Mz-16 BlankEMPA-128/1 Cotton Jeans, Indigo/sulfur black soiled
19GC7-1 Heinz Banana Baby FoodEMPA-130 Cotton dyed with Direct Red 83.1
20GC7-2 Heinz Apple & Banana BFEMPA-131 Acid blue 113 on Polyamide
21GC7-3 Heinz Apple & Mango BFEMPA-132 Cotton dyed with Direct black 22
22GC7-4 Heinz Spaghetti Bolognaise BFEMPA-133 Cotton dyed with Direct blue 71
23GC7-5 Veg. & Turkey (Hipp BF)
24GC7-6 Bisto Gravy
25GC7-7 Coleman's Mustard
26GC7-8 Heinz Sun Dried Tomato Sauce
27GC7-9 Tomato Soup (Campbell's)
28GC7-10 Heinz Spaghetti Sauce
29GC7-11 HP Brown Sauce
30GC7-12 Colemans Beef Casserole
31GC7-13 Homepride Chilli Con Carne
32GC7-14 Asda Chocolate Ice Cream
33GC7-15 Frijj Chocolate Milkshake
34GC7-16 Nestle Chocolate Build Up
35GC8-1 Gerber Banana BF
36GC8-2 Gerber Apple & Banana BF (Mixed Cereal)
37GC8-3 Gerber Apple Cinnamon Oatmeal BF
38GC8-4 Gerber Banana Raspberry Oatmeal BF
39GC8-5 Quaker Original Oatmeal
40GC8-6 Quaker Apples & Cinnamon Oatmeal
41GC8-7 Quaker Cinnamon Spice Oatmeal
42GC8-8 Quaker Maple & Brown Sugar Oatmeal
43GC8-9 Gerber Sweet Potato BF
StainList
 
You could try this user-defined function then.

VBA Code:
Function STACKCOLS(rng As Range) As Variant
  Dim Data As Variant, a As Variant
  Dim r As Long, c As Long, k As Long
 
  Data = rng.Value
  ReDim a(1 To Rows.Count)
  For c = 1 To UBound(Data, 2)
    For r = 1 To UBound(Data, 1)
      If Len(Data(r, c)) > 0 Then
        k = k + 1
        a(k) = Data(r, c)
      End If
    Next r
  Next c
  ReDim Preserve a(1 To k)
  STACKCOLS = Application.Transpose(a)
End Function
This is perfect!!!
Is there a way to use non-contiguous columns, instead of a range? E.g. STACKCOLS(B:B, D:D, G:G)...
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Is there a way to use non-contiguous columns, instead of a range? E.g. STACKCOLS(B:B, D:D, G:G)...
Try this. (It will return an error if there are more than 65,536 items in the stack)

VBA Code:
Function COLSTACK(ParamArray rngs() As Variant) As Variant
  Dim Data As Variant, a As Variant, rng As Variant
  Dim col As Range
  Dim r As Long, k As Long, lr As Long
  
  ReDim a(1 To Rows.Count)
  For Each rng In rngs
    For Each col In rng.Columns
      lr = 0
      On Error Resume Next
      lr = col.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
      On Error GoTo 0
      If lr >= col.Row Then
        Data = col.Resize(lr).Value
        For r = 1 To UBound(Data)
          If Len(Data(r, 1)) > 0 Then
            k = k + 1
            a(k) = Data(r, 1)
          End If
        Next r
      End If
    Next col
  Next rng
  If k < 65537 Then
    ReDim Preserve a(1 To k)
    COLSTACK = Application.Transpose(a)
  Else
    COLSTACK = CVErr(xlErrRef)
  End If
End Function

MossyPants.xlsm
ABCDEFG
1
2
3aadj
4bbek
5ccfln
6dgm
7eh
8fi
9gj
10h
11i
12j
13j
14k
15l
16m
17n
18
Stack Columns (6)
Cell Formulas
RangeFormula
A3:A17A3=COLSTACK(B:B,D3:D15,F:G)
Dynamic array formulas.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,819
Messages
6,181,153
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