VBA - Combine multiple columns into single column

lj97

New Member
Joined
Jul 11, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
G'day 😃


I am needing to combine data from multiple columns into a single column.

The column name/header is in cell A1.

All data present needs to be in Column A.

I am needing each cell to only include one fruit name.

The data set is dynamic and changes regularly (i.e. sometimes there are more columns, sometimes there are less).

If anyone needs more info I am happy to provide it!

Thanks in advance :)(y)

P.s. I have attempted a few things in VBA. I have gotten close, but I am stuck on what to do when there is only one entry in a column (e.g. column F having only "Apple" in this example).

Combine columns into single column.xlsm
ABCDEF
1Fruit NameMelonMangoApplePearApple
2OrangeMelonMangoMangoMango
3ApplePearMangoMango
4PearPearMelonBanana
5ApplePearMelonBanana
6AppleMelonMelon
7AppleMelonMelon
8OrangePearMelon
9AppleMelonBanana
10AppleApple
11MelonMelon
12AppleMelon
13AppleMelon
14ApplePear
15ApplePear
16GrapesPear
17AppleMelon
18AppleMelon
19AppleBanana
20OrangeBanana
21AppleBanana
22AppleMelon
23AppleMelon
24AppleMelon
25ApplePear
26AppleMelon
27AppleApple
28Apple
29Apple
30Apple
31Apple
32Apple
33Apple
34Orange
35Apple
36Apple
37Orange
38Apple
39Grapes
40Orange
41Pear
Sheet1
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
There are six columns. Which are source columns and which is output column?
 
Upvote 0
This might help...
VBA Code:
Sub lj97()
Dim lastrow As Long, lastcol As Long, nextrow As Long, i As Long
lastcol = Range("A1").End(xlToRight).Column
nextrow = Range("A" & Rows.Count).End(xlUp).Row + 1

For i = 2 To lastcol
    lastrow = Cells(Rows.Count, i).End(xlUp).Row
    Cells(nextrow, 1).Resize(lastrow, 1).Value = Cells(1, i).Resize(lastrow, 1).Value
    nextrow = nextrow + lastrow
Next i
End Sub
 
Upvote 0
Solution
You possibly do not have it yet but IF you happen to have the TOCOL function in your version, another way would be ..

VBA Code:
Sub IntoOneColumn()
  With Range("A1").CurrentRegion
    .Resize(.SpecialCells(xlConstants).Count, 1).Value = Evaluate("=TOCOL(" & .Address & ",1,1)")
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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