+ Reply to Thread
Results 1 to 5 of 5

Convert range of cells to comma separated list

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-18-2010
    Location
    Bentley, NM
    MS-Off Ver
    Excel 2010
    Posts
    229

    Convert range of cells to comma separated list

    I need to take the range of cells from C2:C50 and extract each cell's contents and convert them into a list of values separated by commas. The new list can be copied to the clipboard or inserted into a new cell.

    For example: C1 = 50, C2 = 23 ---------convert to "50,23"

    I'm doing this, because I need to use these values as query criteria in Access 07. Usually I will just paste the values into a new table and join on the field(s) needed for criteria, but in this case I think the best way to run this query is to "hardcode" the values into the actual SQL statement.
    Last edited by maw230; 01-23-2012 at 05:57 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,107

    Re: Convert range of cells to comma separated list

    Function ConcatRange(Rng As Range, Optional Separator As String)
    Dim awf As WorksheetFunction: Set awf = WorksheetFunction
    Dim r As Range
    
    For Each r In Rng
        ConcatRange = ConcatRange & awf.Text(r.Value, "") & Separator
    Next
    ConcatRange = Left(ConcatRange, Len(ConcatRange) - Len(Separator))
    End Function

    Regards, TMS
    Last edited by TMS; 01-23-2012 at 05:46 PM. Reason: Amend Function
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    01-18-2010
    Location
    Bentley, NM
    MS-Off Ver
    Excel 2010
    Posts
    229

    Re: Convert range of cells to comma separated list

    Quote Originally Posted by TMShucks View Post
    Function ConcatRange(Rng As Range, Optional Separator As String)
    Dim awf As WorksheetFunction: Set awf = WorksheetFunction
    Dim r As Range
    
    For Each r In Rng
        ConcatRange = ConcatRange & awf.Text(r.Value, "") & Separator
    Next
    ConcatRange = Left(ConcatRange, Len(ConcatRange) - Len(Separator))
    End Function

    Regards, TMS
    Works like a charm! Thanks!

  4. #4
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Convert range of cells to comma separated list

    maw230,

    In Excel VBA you could do somehting like this:

    
    Range("A1") = Join(Application.Transpose(Range("C1:C2")), ",")
    
    Range("A2") = Join(Application.Transpose(Range("C1:C50")), ",")
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,107

    Re: Convert range of cells to comma separated list

    You're welcome. Thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1