+ Reply to Thread
Results 1 to 11 of 11

Output cell values in column to list with comma

  1. #1
    Registered User
    Join Date
    06-26-2014
    Location
    Norway
    MS-Off Ver
    10
    Posts
    43

    Question Output cell values in column to list with comma

    Hello,

    I'm making a macro to run against an sql db.

    I want to make the macro create a string of all the numbers in column A (from row 4 and down) and seperate them by comma

    I have a sheet named "Approval" with the following example-values in column A:
    4: 101
    5: 102
    6: 103

    I want it to output the following:
    "101, 102, 103"

    And this needs to be used by the macro and inputed as this:
    Please Login or Register  to view this content.
    How can I make the values be all of the numbers in column A4 and down seperated by comma?

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Output cell values in column to list with comma

    Please Login or Register  to view this content.
    Not tested as not in excel.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    06-26-2014
    Location
    Norway
    MS-Off Ver
    10
    Posts
    43

    Re: Output cell values in column to list with comma

    OK, I decided to do a different approach.

    I will make a separate macro to generate the list before running the sql-macro.

    So I now need a stand-alone macro to this:
    Generate comma separated list in cell I1 on sheet "VBdata" based on all numbers in column A (from row 4 and down) in sheet "Aproval".

    Example
    The sheet "Aproval" has the following data in column A:
    101
    102
    103
    104

    Result is paste in cell I1 on sheet "VBdata" like this:
    101, 102, 103, 104

    How to make a stand-alone macro to achive this?
    Last edited by vonrainer; 06-30-2014 at 07:20 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Output cell values in column to list with comma

    mine, with range("i1").value=strSQL

  5. #5
    Registered User
    Join Date
    06-26-2014
    Location
    Norway
    MS-Off Ver
    10
    Posts
    43

    Re: Output cell values in column to list with comma

    I can not seem to get this to work.

    Could you please paste the complete vba to get the numbers in:
    Sheet "Aproval"
    Column: A
    Row: 4 and down

    Pasted in:
    Sheet "VBdata"
    Cell: I1

    And values separated by comma?

  6. #6
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Output cell values in column to list with comma

    that does it

  7. #7
    Registered User
    Join Date
    06-26-2014
    Location
    Norway
    MS-Off Ver
    10
    Posts
    43

    Re: Output cell values in column to list with comma

    Well, here is what i've tried so far with no success:

    Get runtime error 424: object required on the marked yellow line

    Please Login or Register  to view this content.
    Last edited by vonrainer; 06-30-2014 at 09:07 AM.

  8. #8
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Output cell values in column to list with comma

    For Each c In Worksheets("Approval").Range("A4:A" & Worksheets("Approval").UsedRange.Rows.Count).Cells
    If c = Worksheets("Approval").UsedRange.Rows.Count Then
    strSQl = strSQl & c.Value
    Else
    strSQl = strSQl & c.Value & ","
    End If
    Next c

    Range("i1").Value = strSQl


    Fixed, its polite to have a go at fixing things yourself, no need for a with block on one line.

  9. #9
    Registered User
    Join Date
    06-26-2014
    Location
    Norway
    MS-Off Ver
    10
    Posts
    43

    Re: Output cell values in column to list with comma

    Thank you! That worked like a charm, however i get a lot of values at the end which are blank and separated by comma.

    Example:
    101, 102, 103,,,,,,,,

    Tried to add an if statement to check if c.value > 100 and then exit sub but that did not solve the issue =\
    I'm really trying my best to do it on my own and learn, but unfortunately still a noob

  10. #10
    Registered User
    Join Date
    06-26-2014
    Location
    Norway
    MS-Off Ver
    10
    Posts
    43

    Re: Output cell values in column to list with comma

    YES! Fiddling with the code gave me the desired result I would like to thank nathansav for his guidance to solving my issue.

    I changed the formula to only add values above 100 to the string, and then deleting the last character before entering the value to cell I1.
    Below is the code that worked:

    Please Login or Register  to view this content.

  11. #11
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Output cell values in column to list with comma

    Good chap. You can answer a post on how to do it now

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Compare comma separated values in a cell to a list
    By SMB in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-03-2019, 01:10 AM
  2. Comma Separated Cell Values to Column of Unique Cell Values
    By Nuggetross in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 04-02-2014, 07:24 PM
  3. Output a comma separated list of inclusive years
    By sigmaj in forum Excel General
    Replies: 5
    Last Post: 07-02-2013, 11:09 AM
  4. [SOLVED] Extracting information from a comma separated list of values in one cell
    By cardiff in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-11-2012, 09:17 PM

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