Conversion of column list to comma delimited string

rn119

New Member
Joined
Feb 27, 2013
Messages
47
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.
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,284
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
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,199
Office Version
2013
Platform
Windows
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
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
42,256
Office Version
365
Platform
Windows
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:

rn119

New Member
Joined
Feb 27, 2013
Messages
47
Thanks Michael/Peter. Both of these macros worked great.
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,199
Office Version
2013
Platform
Windows
@Peter_SSs
I'm ever impressed...:pray:
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
42,256
Office Version
365
Platform
Windows
@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
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,199
Office Version
2013
Platform
Windows
Aww stop it, now you're just making me feel redundant..:oops:
Thanks for the pointer.
 

rn119

New Member
Joined
Feb 27, 2013
Messages
47
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:

Forum statistics

Threads
1,082,259
Messages
5,364,103
Members
400,779
Latest member
lumers

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top