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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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
Hello
Sorry. I am a new member. I would like to ask something...
I have the same problem in Excel 2007.
i would like a tocol function in vba but the above code doesn't work in my case...
Any idea????
I want the values of cells A1 to C8 (or 800 ex) of a table to be copied to a new column
 
Upvote 0
Welcome to the MrExcel board!

Try something like this with a copy of your workbook.

VBA Code:
Sub To_col()
  Dim c As Range
  
  For Each c In Range("A1:C8").Columns
    c.Copy Destination:=Range("F" & Rows.Count).End(xlUp).Offset(1)
  Next c
End Sub
 
Upvote 0
Thank you!! I put it in vba but could you tell me the steps?? This code doesn't return any function...
 
Upvote 0
I put it in vba
Where in vba? It should go in a standard module. Like this

1733392542980.png


Then in your worksheet area press Alt+F8, choose the To-col macro and click 'Run'.

Here is my sample data and the results of the code in column F

seniko.xlsm
ABCDEF
1624283351
26684648624
37249228526
4720589139724
5351451266720
6117107521351
7237233115117
8676888411237
9676
10283
11684
12922
13589
14451
15107
16233
17888
18351
19648
20852
21139
22266
23521
24115
25411
26
Sheet1
 
Upvote 0
Thank you very much!! It works!! In column F only but -at least- it works!! I would like to have an option for the list to start in the cell I select and below, but that's fine too!!
Tnx!!!
 
Upvote 0
I would like to have an option for the list to start in the cell I select and below
Select the starting cell for the results and then run this code.
VBA Code:
Sub To_col_v2()
  Dim c As Range
  Dim lr As Long
  
  For Each c In Range("A1:C8").Columns
    lr = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
    If lr <= ActiveCell.Row Then lr = ActiveCell.Row - 1
    c.Copy Destination:=Cells(lr + 1, ActiveCell.Column)
  Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
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