+ Reply to Thread
Results 1 to 3 of 3

Can't populate an array with selected items from listbox

Hybrid View

  1. #1
    Registered User
    Join Date
    05-26-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    49

    Can't populate an array with selected items from listbox

    I am trying to take selected items from a listbox and put the selected items into an array. Basically I am trying to put the selected items into a variable that I can pass to other modules. I am close but something is off. I can't tell if it's the variable declaration or the code. I've searched the internet and tried many of the options out there but none work for me. I appreciate any insight or help. Perhaps I should be doing something completely different. Thank you.

    I get Run Time error 91. Object variable or with block variable not set

    Here's what I have so far...

    Dim x as integer
    Dim i as integer
    Dim myArray() as variant
    
    x = 0
        For i = 0 To Me.lstbox1.ListCount - 1
            If Me.lstbox1.Selected(i) = True Then
                ReDim Preserve myArray(x)
                myArray(x) = lstbox1.List(i)
                x = x + 1
            End If
        Next i

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Can't populate an array with selected items from listbox

    Hi,

    I've made some very minor changes to your code and tried it without problems :

    Private Sub CommandButton1_Click()
        Dim x As Integer
        Dim i As Integer
        Dim myArray() As Variant
        
        x = 0
        For i = 0 To Me.ListBox1.ListCount - 1
            If Me.ListBox1.Selected(i) = True Then
                ReDim Preserve myArray(x)
                myArray(x) = ListBox1.List(i)
                x = x + 1
            End If
        Next i
    
    End Sub
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Registered User
    Join Date
    05-26-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Can't populate an array with selected items from listbox

    Hmmm...I tried that earlier and it didn't work...yet this time it worked...after I undeclared the ListBox1. Thank you for the quick response! I really appreciate!

+ 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. Copy Selected items from multicolumn, multiselect listbox to another listbox
    By Willigb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-19-2013, 11:27 AM
  2. Transfer random listbox items to new listbox and then loop through selected items
    By narrowgate88 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-31-2013, 05:58 PM
  3. Populate userform listbox based on value selected in another listbox
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-07-2012, 05:16 PM
  4. [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
  5. ListBox Selected Items into an Array
    By jtp550 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-27-2005, 07:44 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