+ Reply to Thread
Results 1 to 2 of 2

Excel 2007 : One column to txt comma separated values

Hybrid View

  1. #1
    Registered User
    Join Date
    11-11-2009
    Location
    Washington, DC
    MS-Off Ver
    Excel 2007
    Posts
    19

    One column to txt comma separated values

    Hi, I'm thinking this shouldn't be as difficult as its become, but i have one column of data, A1:A501 (including header) with numbers in them. I need to convert this column to a comma seperated string to run as part of code in SQL. Can someone help with some dumbed-down intstructions? Thanks in advance...

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: One column to txt comma separated values

    Add this code to your VB editor (Alt+F11, Insert|Module)

    Function aconcat(a As Variant, Optional sep As String = "") As String
    ' Harlan Grove, Mar 2002
    Dim y As Variant
    
    If TypeOf a Is Range Then
    For Each y In a.Cells
    aconcat = aconcat & y.Value & sep
    Next y
    ElseIf IsArray(a) Then
    For Each y In a
    aconcat = aconcat & y & sep
    Next y
    Else
    aconcat = aconcat & a & sep
    End If
    
    aconcat = Left(aconcat, Len(aconcat) - Len(sep))
    End Function

    Then apply formula in a separate column

    =SUBSTITUTE(TRIM(aconcat(A1:A501," "))," ",",")

    You can then copy this and paste special >> Values over itself to make it a hard-coded string.
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

+ 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