+ Reply to Thread
Results 1 to 12 of 12

How to convert numeric data in an excel column into a string of comma values.

  1. #1
    Registered User
    Join Date
    07-22-2009
    Location
    Belmopan, Belize
    MS-Off Ver
    Excel 2003
    Posts
    7

    Question How to convert numeric data in an excel column into a string of comma values.

    I have never done any excel programming before. I need to create a comma separated list from an excel column for use in an SQL server application that I am developing.

    eg column:
    1
    5
    7
    9

    should produce the string '1,5,7,9'

    I could of course do this manually but there are so very many worksheets with so many rows that would make that task so very boring never mind error prone.

    Please help!

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: How to convert numeric data in an excel column into a string of comma values.

    Hi Desgordon,

    Please be aware that a cell (in Excel 2003) can only hold 32,767 characters and will only display about 1000. (http://office.microsoft.com/en-us/ex...005199291.aspx) It may be an issue if you have "so many rows" that it fills the cell to the limit.

    For less than the limit, you could use something like the following - assuming your data was in column A:
    Please Login or Register  to view this content.

  3. #3
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: How to convert numeric data in an excel column into a string of comma values.

    An alternative to looping through possibly thousands of rows might be to dump the range into an array and process it that way, then spit out the result as one string. (Same result, just different method.)
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    07-22-2009
    Location
    Belmopan, Belize
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: How to convert numeric data in an excel column into a string of comma values.

    Hi Paul,

    Thank you very much for your responses. Your second suggestion works perfectly while the first seems to repeat the numbers in the array. So I have chosen to use the second. But there is one other problem that you may be able to help with: the worksheets seems to have many hidden rows and I don't want the data from those rows in the string.

    How can I ignore the hidden rows?

  5. #5
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: How to convert numeric data in an excel column into a string of comma values.

    The first code snippet shouldn't duplicate anything. It should work the same way as the second, just a different method as stated. (Same results in my book.) In order to skip the hidden rows, you'll likely have to use the first option with a slight adjustment. I suppose you could build an extra array into the second option that contains a 1 if a row is hidden, 0 if not, but then you'd still be looping through the rows anyway.
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    07-22-2009
    Location
    Belmopan, Belize
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: How to convert numeric data in an excel column into a string of comma values.

    Hi Paul,

    You're right about the duplication bit. The problem is that the function is reading from Column A which has duplicated values and is not the column I want, so I made the modifications shown below hoping that it would read from Column B instead:

    Please Login or Register  to view this content.
    But its still reading from Column A. Any ideas why.
    Last edited by Paul; 03-29-2012 at 06:03 PM. Reason: Added CODE tags for new user.

  7. #7
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: How to convert numeric data in an excel column into a string of comma values.

    Yes, because "Cells(i, 1)" refers to the cell in row "i", column 1 (which is "A"). Change any instances of Cells(i, 1) to Cells(i, 2) and you should be set.

    Also, please be sure to wrap your code within [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags in the future, per the forum rules. I've done it for you this time.

    Thanks!

  8. #8
    Registered User
    Join Date
    07-22-2009
    Location
    Belmopan, Belize
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: How to convert numeric data in an excel column into a string of comma values.

    Actually I had tried the cells(i,2) reference while waiting for your reply and subsequently but it still did the same thing i.e. return column A values concatenated. I then deleted column A (without saving) as a workaround and it works fine.

    And thanks for the tip about the
    Please Login or Register  to view this content.
    tags. I shall not forget to include them in the future.

  9. #9
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: How to convert numeric data in an excel column into a string of comma values.

    Note that there were two instances of "Cells(i, 1)" in the last code I provided which needed to be changed. If you didn't change the second one, it would still have been grabbing values from column 1 (A). Hopefully that's it sorted now.

  10. #10
    Registered User
    Join Date
    07-22-2009
    Location
    Belmopan, Belize
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: How to convert numeric data in an excel column into a string of comma values.

    You are right. It works perfectly now.

    Thank you very much.

  11. #11
    Registered User
    Join Date
    04-02-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: How to convert numeric data in an excel column into a string of comma values.

    if u dont mind where i write above code ............ i am new to this.........

  12. #12
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: How to convert numeric data in an excel column into a string of comma values.

    You would put that code into the worksheet module that contains the data you want modified.

    Right-click on the sheet tab that has your data and choose View Code. Copy the code above and paste it into the VB Editor window that appears. Close the VB Editor. Run the macro by pressing ALT+F8 to open the Macros dialog.

    Note that the code is specific to cells and columns that the original poster requested. If your needs are different please start your own thread. You can reference this thread via hyperlink if necessary.

+ 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