Horizontal merge into new cell

Westbury

Board Regular
Joined
Jun 7, 2009
Messages
149
How do I use vba to merge the contents of 3 columns on one row into a new cell on that row with each former cell separated with a comma and the last entry in quotation marks?

e.g.

AA BB CC would merge to AA,BB,"CC" in a new cell and the process repeats for n rows

I've found lots of ideas to create a single column from data but not to execute a horizontal merge.

Thanks
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this formula: =A1&","&B1&","&""""&C1&""""
 
Upvote 0
How about
Code:
   Range("C1") = Range("A1") & "," & Range("B1") & ",""" & Range("C1") & Chr(34)
 
Upvote 0
If you are trying to do it on an entire column try
Code:
Sub Westbury()
   Dim Cl As Range
   Dim x As Variant
   For Each Cl In Range("D2", Range("C" & Rows.Count).End(xlUp).Offset(, 1))
      x = join(Application.Index(Cl.Offset(, -3).Resize(, 2).Value, 1, 0), ",")
      Cl = x & ",""" & Cl.Offset(, -1) & Chr(34)
   Next Cl
End Sub
 
Upvote 0
Thanks, this works.

Last question on this(!) How could I put the output on a new worksheet in the same file?
 
Upvote 0
Which of the two codes are you referring to?
 
Upvote 0
Assuming that you are talking about the loop, try
Code:
Sub Westbury()
   Dim Ary As Variant, x As Variant
   Dim i As Long
   With Sheets("Sheet1")
      Ary = .Range("A1", .Range("A" & Rows.Count).End(xlUp).Offset(, 3)).Value2
   End With
   For i = 1 To UBound(Ary)
      x = join(Application.Index(Ary, i, Array(1, 2)), ",")
      Sheets("Sheet2").Range("A" & i) = x & ",""" & Ary(i, 3) & Chr(34)
   Next i
End Sub
 
Upvote 0
How about
Code:
   Range("C1") = Range("A1") & "," & Range("B1") & ",""" & Range("C1") & Chr(34)
Given you used quotes everywhere else, why use Chr(34) at the end (replacing it as below saves 3 characters)?

Range("C1") = Range("A1") & "," & Range("B1") & ",""" & Range("C1") & """"
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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