+ Reply to Thread
Results 1 to 20 of 20

ListBox - Copy all selected options into a single cell seperated by commas

  1. #1
    Registered User
    Join Date
    03-30-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    18

    Question ListBox - Copy all selected options into a single cell seperated by commas

    Hi,

    I've got a userform with a listbox that i can select from multiple entries.

    I need all the selected entries to be populated into the selected cell (user form opens when a cell is double clicked, therefore remains as the active cell) seperated by commas.

    I.e. When cell J5 is double clicked, the userfor pops up, they select which options are required, click close and all the options are written into the active acell seperated by commas.

    Additionally, if the cell is double clicked again, i'd like the the options to remain selected.

    This is what i've come up with so for.

    Thanks
    AusTempest.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    can you attach a sample file with data and desired result ?
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    03-30-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    Damn, I thought that attached.

    Apologies.

    I'll boot my computer up and attach it

    Sent from my One using Tapatalk

  4. #4
    Registered User
    Join Date
    03-30-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    Example workbook attached.

    For Example: If i double click in J5, my userform opens. If i select "ARH", "MRH" and "C-130", then clikc the OK and close button, i want cell J5 to be populated with "ARH, MRH, C-130" (order doesn't matter).

    I also would like the three options to be reselected if the cell is double clicked again, so that if changes are neccesary, everything doesn't need to be selected all over again.
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    03-30-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    Awesome, thanks for that.

    There's a comma at the end, but i don't really care about that.

    Is there anyway to keep whats selected when re clicked?

  7. #7
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    You may try:
    Please Login or Register  to view this content.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  8. #8
    Registered User
    Join Date
    03-30-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    That cuts off the first two letters of the output, not the last.

    I.e. selecting "squirrel and "PC-9" gives ", uirrel, PC-9, "

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Put the selected values in an array, use Join to create a comma separated list from the array and then write the list to the cell.

    To have the values in the cell selected when the userform opens read the list from the cell and use Split to separate the items into an array.

    Loop through that array and use Application.Match to find the location of each item in the listbox and select it.
    If posting code please use code tags, see here.

  10. #10
    Registered User
    Join Date
    03-30-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    HAHA !

    Minorly changed you addition to remove the far right two characters instead (and also remove the ", " at the start if its blank)

    Please Login or Register  to view this content.

  11. #11
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    Quote Originally Posted by AusTempest View Post
    That cuts off the first two letters of the output, not the last.

    I.e. selecting "squirrel and "PC-9" gives ", uirrel, PC-9, "
    I do not think you used all of the code I provided. However, it must be changed for empty cells to avoid starting cell with ", ":
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    03-30-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    Quote Originally Posted by Izandol View Post
    I do not think you used all of the code I provided. However, it must be changed for empty cells to avoid starting cell with ", ":
    Thanks for that. I've got the list box outputting correctly now.

    Quote Originally Posted by Norie View Post
    Put the selected values in an array, use Join to create a comma separated list from the array and then write the list to the cell.

    To have the values in the cell selected when the userform opens read the list from the cell and use Split to separate the items into an array.

    Loop through that array and use Application.Match to find the location of each item in the listbox and select it.
    I would love to understand what your talking about, but i'm afraid it's a tad over my head.

  13. #13
    Registered User
    Join Date
    03-30-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    This attachment is wht i've got so far.

    I've modified the user form to list all in the left ListBox, and show added in the right ListBox.

    In this scenario, i'd need the contents of the active cell to populate the right ListBox (Listbox2)

    I've been googling what Norie said, but i can't seem to get my head around it.
    Attached Files Attached Files

  14. #14
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    You may try:
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    03-30-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    Quote Originally Posted by Izandol View Post
    You may try:
    Please Login or Register  to view this content.
    You Sir, are a life saver.

    It works perfectly.

    I've gone ahead and coded the two list boxes so that as one item is added to listbox2, its removed from listbox1, and vice versa, all good.

    The only thing left is that when i first open the userform and use your code to populate listbox2 with what is already in the cell, i don't want those to start in list box one.

    i use the following to populate listbox1 on opening

    Please Login or Register  to view this content.

  16. #16
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    You may remove matching items:
    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    03-30-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    This is what i've got so far:

    Please Login or Register  to view this content.
    I've tried to select the elements of listbox2 and then remove them from listbox1 after both listboxes have been initially filled.

    All i get is an opening error.

    Is it possible to select and remove items before the Initialise sub has completed?

  18. #18
    Registered User
    Join Date
    03-30-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    Thank you Izandol, thank you very much.

    All done now.

    Just curious though, do you know why my attempt didnt work? (and yes, i know your is a far better result)

  19. #19
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    RemoveItem requires an index number not an item in the listbox list and so you must match the item to get a position in list.

  20. #20
    Registered User
    Join Date
    03-30-2014
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: ListBox - Copy all selected options into a single cell seperated by commas

    That does make sense.

    P.S. For anyone else finding this thread in the months / years to come, here is a copy of the final workbook.
    Attached Files Attached Files

+ 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. Multiple numbers seperated by commas in a single cell
    By abe10 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-26-2013, 05:42 PM
  2. copy listbox multiitems selected in a single excell cell
    By zovan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-20-2012, 06:36 AM
  3. Options marked with an "X" listed in a single cell separated by commas
    By jpecor2 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-13-2011, 02:06 PM
  4. Replies: 5
    Last Post: 07-03-2006, 10:19 AM
  5. Replies: 2
    Last Post: 04-03-2006, 05:50 AM

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