+ Reply to Thread
Results 1 to 8 of 8

Pass multiple selected items from ListBox to array, join array items, then pass to cell

  1. #1
    Registered User
    Join Date
    02-22-2017
    Location
    Singapore
    MS-Off Ver
    2010
    Posts
    9

    Pass multiple selected items from ListBox to array, join array items, then pass to cell

    I'm trying to store userform values in each cell.

    Listboxes have multiselection enabled. I want to store all of them in the same cell, comma-delimited.
    What I'm trying to do here is to first store all selected items in the listbox into an array, then join them up and pass the values into the cell.

    Here is a snippet of my code:

    Please Login or Register  to view this content.
    The commented line,
    Please Login or Register  to view this content.
    does not work, and return a runtime '1004' error.

    What is wrong here, and what should i do?

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Pass multiple selected items from ListBox to array, join array items, then pass to cel

    Perhaps,

    Move that line to below MsgBox, otherwise result of i = i - 1 becomes 0.

  3. #3
    Registered User
    Join Date
    02-22-2017
    Location
    Singapore
    MS-Off Ver
    2010
    Posts
    9

    Re: Pass multiple selected items from ListBox to array, join array items, then pass to cel

    Quote Originally Posted by jindon View Post
    Perhaps,

    Move that line to below MsgBox, otherwise result of i = i - 1 becomes 0.
    done it and it works! thanks!

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Pass multiple selected items from ListBox to array, join array items, then pass to cel

    There is no need to use an array. This uses a MsgBox (as does the OP) since I can't see where the data is supposed to go.

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Pass multiple selected items from ListBox to array, join array items, then pass to cel

    peejayjay

    Just curious, why are you looping through the listbox twice?
    If posting code please use code tags, see here.

  6. #6
    Registered User
    Join Date
    02-22-2017
    Location
    Singapore
    MS-Off Ver
    2010
    Posts
    9

    Re: Pass multiple selected items from ListBox to array, join array items, then pass to cel

    Quote Originally Posted by Norie View Post
    peejayjay

    Just curious, why are you looping through the listbox twice?
    I used a dynamic array to store the selected values in the listbox. The first loop is to count the number of selected items from the listbox and ReDim the array size to the number of selected items.

    I'm pretty new to VBA and sorry if my code isn't the most efficient.

  7. #7
    Registered User
    Join Date
    02-22-2017
    Location
    Singapore
    MS-Off Ver
    2010
    Posts
    9

    Re: Pass multiple selected items from ListBox to array, join array items, then pass to cel

    Quote Originally Posted by mikerickson View Post
    There is no need to use an array. This uses a MsgBox (as does the OP) since I can't see where the data is supposed to go.

    Please Login or Register  to view this content.
    thanks! simple and saved me lines of code.

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Pass multiple selected items from ListBox to array, join array items, then pass to cel

    Arrays and strings (or delimited strings) are very, very similar. They are items (elements, characters (or sub-strings)) in order.

+ 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. How to pass user selected list items from listbox to another procedure in excel using vba
    By mahendra.asapu in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-20-2016, 03:51 PM
  2. [SOLVED] Storing selected items index from multiselect listbox into array
    By Pamela2411 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-08-2015, 06:55 PM
  3. [SOLVED] Can't populate an array with selected items from listbox
    By jamesdeuce in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-16-2014, 09:57 PM
  4. Replies: 0
    Last Post: 08-17-2012, 07:00 AM
  5. [SOLVED] Populating an array with selected ListBox items
    By wz4np1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-13-2012, 07:52 AM
  6. ListBox Selected Items into an Array
    By jtp550 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-27-2005, 07:44 PM
  7. Select multiple listbox items and pass to an array
    By golzilla in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-13-2005, 05:49 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