VBA Merge cells comma delimited

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
Hi, i am trying to merge cells together comma delimited, I am almost there but need help, Can someone help with this please.

Code:
Sub MergeCommaDel()
'
    Dim objSelection, objCell As range, MyStr As String
    
    Application.ScreenUpdating = False
    On Error Resume Next
    Set objSelection = Intersect(Selection, ActiveSheet.UsedRange)
    
    For Each objCell In objSelection
            MyStr = MyStr & VBA.Trim$(objCell) & ","
    Next
    
    With ActiveWindow
        .Selection(1, 1).NumberFormat = "@"
        .Selection(1, 1).Value = MyStr
    End With
    
End Sub

This is what I end up with so far
a,b,c,d,bcd

<tbody>
</tbody>

But What I would like to do is this

From This
abcd

<tbody>
</tbody>


To This

a,b,c,d

<tbody>
</tbody>
 
Hi all, The codes posted does this for cells but how would i get the same results if columns are selected, any help would be appreciated

From This

abcde
fghij
klmno
pqrst

<tbody>
</tbody>


To This


a,b,c,d,e
f,g,h,i,j
k,l,m,n,o
p,q,r,s,t

<tbody>
</tbody>

Select all of the cells you want to process (assumed to be A1:E4 for the above example although the following code will work no matter where the values are located) and then give this macro a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub MergeCommaDelimit()
  Dim Cell As Range
  For Each Cell In Selection.Columns(1).Cells
    Cell.Value = Join(Application.Index(Selection.Value, Cell.Row - Selection.Row + 1, 0), ", ")
  Next
  Selection.Offset(, 1).Resize(, Selection.Columns.Count - 1).ClearContents
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,215,981
Messages
6,128,095
Members
449,419
Latest member
mammothzaa

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