+ Reply to Thread
Results 1 to 5 of 5

Thread: Convert range of cells to comma separated list

  1. #1
    Registered User
    Join Date
    01-18-2010
    Location
    Bentley, NM
    MS-Off Ver
    Excel 2007
    Posts
    89

    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 04:57 PM.

  2. #2
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,233

    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 TMShucks; 01-23-2012 at 04:46 PM. Reason: Amend Function

  3. #3
    Forum Guru
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2003, 2007.
    Posts
    1,462

    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
    stanleydgromjr
    Windows Vista Business, Excel 2003 and 2007

    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.

  4. #4
    Registered User
    Join Date
    01-18-2010
    Location
    Bentley, NM
    MS-Off Ver
    Excel 2007
    Posts
    89

    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!

  5. #5
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,233

    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.2.0