Page 1 of 3 123 LastLast
Results 1 to 10 of 29

Thread: Conversion of column list to comma delimited string
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Feb 2013
    Posts
    47
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Conversion of column list to comma delimited string

    Is there a macro I can use that would convert this...

    USA
    Germany
    Chile
    France
    Turkey
    Indonesia

    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.

  2. #2
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,820
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

    Default Re: Conversion of column list to comma delimited string

    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
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please Iím not perfect yet. "Memories are forever"

  3. #3
    Board Regular Michael M's Avatar
    Join Date
    Oct 2005
    Location
    South Western NSW
    Posts
    17,808
    Post Thanks / Like
    Mentioned
    18 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Conversion of column list to comma delimited string

    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
    Regards
    Michael M
    ---------------------------------------
    The more I learn, the less I seem to know.....A Please and Thank You cost nothing !
    It's easier to debug if we can see the whole macro !
    Home 2007 & 2013

    - Posting guidelines, forum rules and terms of use

    - To download Mr Excel HTML Maker

    - Try searching for your answer first, see how

    - Read the FAQs

    - List of BB codes


    [CODE]Place Your Code Here[/CODE]

  4. #4
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    40,976
    Post Thanks / Like
    Mentioned
    90 Post(s)
    Tagged
    21 Thread(s)

    Default Re: Conversion of column list to comma delimited string

    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 by Peter_SSs; Sep 22nd, 2019 at 01:38 AM.
    Hope this helps, good luck.
    Peter
    Excel 365 - Windows 10
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the # key in the Reply window
    - Read: Forum Rules, Forum Use Guidelines, & FAQ

  5. #5
    New Member
    Join Date
    Feb 2013
    Posts
    47
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Conversion of column list to comma delimited string

    Thanks Michael/Peter. Both of these macros worked great.

  6. #6
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    40,976
    Post Thanks / Like
    Mentioned
    90 Post(s)
    Tagged
    21 Thread(s)

    Default Re: Conversion of column list to comma delimited string

    Quote Originally Posted by rn119 View Post
    Thanks Michael/Peter. Both of these macros worked great.
    You're welcome.
    Hope this helps, good luck.
    Peter
    Excel 365 - Windows 10
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the # key in the Reply window
    - Read: Forum Rules, Forum Use Guidelines, & FAQ

  7. #7
    Board Regular Michael M's Avatar
    Join Date
    Oct 2005
    Location
    South Western NSW
    Posts
    17,808
    Post Thanks / Like
    Mentioned
    18 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Conversion of column list to comma delimited string

    @Peter_SSs
    I'm ever impressed...
    Regards
    Michael M
    ---------------------------------------
    The more I learn, the less I seem to know.....A Please and Thank You cost nothing !
    It's easier to debug if we can see the whole macro !
    Home 2007 & 2013

    - Posting guidelines, forum rules and terms of use

    - To download Mr Excel HTML Maker

    - Try searching for your answer first, see how

    - Read the FAQs

    - List of BB codes


    [CODE]Place Your Code Here[/CODE]

  8. #8
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    40,976
    Post Thanks / Like
    Mentioned
    90 Post(s)
    Tagged
    21 Thread(s)

    Default Re: Conversion of column list to comma delimited string

    Quote Originally Posted by Michael M View Post
    @Peter_SSs
    I'm ever impressed...
    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.
    Code:
    Cells(1, 2).Value = Mid(s, 2)
    Quote Originally Posted by Michael M View Post
    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
    Hope this helps, good luck.
    Peter
    Excel 365 - Windows 10
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the # key in the Reply window
    - Read: Forum Rules, Forum Use Guidelines, & FAQ

  9. #9
    Board Regular Michael M's Avatar
    Join Date
    Oct 2005
    Location
    South Western NSW
    Posts
    17,808
    Post Thanks / Like
    Mentioned
    18 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Conversion of column list to comma delimited string

    Aww stop it, now you're just making me feel redundant..
    Thanks for the pointer.
    Regards
    Michael M
    ---------------------------------------
    The more I learn, the less I seem to know.....A Please and Thank You cost nothing !
    It's easier to debug if we can see the whole macro !
    Home 2007 & 2013

    - Posting guidelines, forum rules and terms of use

    - To download Mr Excel HTML Maker

    - Try searching for your answer first, see how

    - Read the FAQs

    - List of BB codes


    [CODE]Place Your Code Here[/CODE]

  10. #10
    New Member
    Join Date
    Feb 2013
    Posts
    47
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Conversion of column list to comma delimited string

    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 by rn119; Sep 23rd, 2019 at 12:54 PM.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •