Conversion of column list to comma delimited string

rn119

New Member
Joined
Feb 27, 2013
Messages
49
Is there a macro I can use that would convert this...

USA
Germany
Chile
France
Turkey
Indonesia

<tbody>
</tbody>

to this....

USA,Germany,Chile,France,Turkey,Indonesia

The column will have always data in Column A and rows could vary in terms of data input.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
So in this example you have:

A1 USA
A2 Germany

Etc.

So where do you want the results entered?


Would it be B1:

USA,Germany
 
Upvote 0
Try using

Code:
Sub MM1()
Dim i As Integer, s As String
i = 1
Do Until Cells(i, 1).Value = ""
    If s = "" Then
        s = Cells(i, 1).Value
    Else
        s = s & "," & Cells(i, 1).Value
    End If
    i = i + 1
Loop
Cells(1, 2).Value = s
End Sub
 
Upvote 0
Assuming the list in column A starting at A2, try
Code:
Sub Comma_List()
  Range("B2").Value = Join(Application.Transpose(Range("A2", Range("A" & Rows.Count).End(xlUp))), ",")
End Sub

Although you asked for a macro, this can also be done by formula if your Excel version has the TEXTJOIN function

=TEXTJOIN(",",1,A2:A7)
 
Last edited:
Upvote 0
@Peter_SSs
I'm ever impressed...:pray:
Thanks Michael. :)

BTW, if you were to use looping you could compact this coloured part considerably by using only the red line. All that means is that s ends up being a string beginning with a comma which you can easily deal with when writing the result to the sheet by simply omitting that first character. :)
Rich (BB code):
Cells(1, 2).Value = Mid(s, 2)

Try using

Code:
Sub MM1()
Dim i As Integer, s As String
i = 1
Do Until Cells(i, 1).Value = ""
 [COLOR="#0000FF"]   If s = "" Then
        s = Cells(i, 1).Value
    Else
        [COLOR="#FF0000"]s = s & "," & Cells(i, 1).Value[/COLOR]
    End If[/COLOR]
    i = i + 1
Loop
Cells(1, 2).Value = s
End Sub
 
Upvote 0
Aww stop it, now you're just making me feel redundant..:oops:
Thanks for the pointer.
 
Upvote 0
Michael/Peter...one more question here. Can we concatenate open and close brackets to this macro? So basically, the end result would look like...

(USA,Germany,Chile,France,Turkey,Indonesia)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,113,998
Members
448,539
Latest member
alex78

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