Macro to combine columns without formulas

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Good Morning all,

I need a macro to combine any columns I select into one column, so if I chose columns A,B,C,D then all data from these columns will be merged into column A with a space, or if I selected Columns D to T then all data from columns D to T would be merged into column D with a space,

Each column must keep the original data on the same row and the columns originally selected are deleted after merging.
Each Column will have varied data with different number of rows
Headers are to be included with this merge
I am using Excel 2007

(Note: the full stops used don't mean anything apart from to break up text)

Example

.......A........B.........C.........D..........E
1 Hi.......How......Are......You......Hello
2 ..........How
3 ......................Are
4 ................................You

Merged to this

.............A...................B.........C........D.........E
1 Hi How Are You.....Hello
2 How
3 Are
4 You

Any help is appreciated,

Thanks in advance
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Test in a copy of your workbook

Rich (BB code):
Sub Combine()
  Dim a, b
  Dim i As Long
  
  With Intersect(ActiveSheet.UsedRange, Selection)
    a = .Value
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    For i = 1 To UBound(a, 1)
      b(i, 1) = Application.Trim(Join(Application.Index(a, i, 0)))
    Next i
    .Value = b
    .EntireColumn.AutoFit
  End With
End Sub
 
Last edited:
Upvote 0
Hi Peter, this code does exactly what I need, However can it be modified so it deletes the empty columns as well, so in the case of the example previously posted Columns A to D had data in, after the merge columns B to D were empty but still present. thanks
 
Upvote 0
Hi Peter, this code does exactly what I need, However can it be modified so it deletes the empty columns as well, so in the case of the example previously posted Columns A to D had data in, after the merge columns B to D were empty but still present. thanks
Sure

Rich (BB code):
Sub Combine()
  Dim a
  Dim i As Long
  
  With Intersect(ActiveSheet.UsedRange, Selection)
    a = .Value
    For i = 1 To UBound(a, 1)
      a(i, 1) = Application.Trim(Join(Application.Index(a, i, 0)))
    Next i
    .Value = a
    .Columns(1).EntireColumn.AutoFit
    .Offset(, 1).Resize(UBound(a, 2) - 1).EntireColumn.Delete
  End With
End Sub
 
Upvote 0
Hi Peter, the delete code part doesn't quite work, It deletes columns B to E rather than B to D.
Thanks
 
Upvote 0
Hi Peter, the delete code part doesn't quite work, It deletes columns B to E rather than B to D.
Thanks
Sorry, I was missing a comma
Rich (BB code):
.Offset(, 1).Resize(, UBound(a, 2) - 1).EntireColumn.Delete
 
Upvote 0
Works Perfectly Peter, Many thanks for your help
 
Upvote 0

Forum statistics

Threads
1,215,902
Messages
6,127,648
Members
449,394
Latest member
fionalofthouse

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