+ Reply to Thread
Results 1 to 2 of 2

Populating an array with selected ListBox items

Hybrid View

  1. #1
    Registered User
    Join Date
    03-02-2008
    Posts
    39

    Populating an array with selected ListBox items

    I am trying to populate an array with the items a user selected in a userform listbox. Below is my current code but it is not working, I keep getting a subscript out of range error on the following line "SelItems(k) = .List(i)"

    Dim lCount As Integer
    For i = 0 To ListBox1.ListCount - 1
            'check if the row is selected and add to count
        If ListBox1.Selected(i) Then lCount = lCount + 1
    Next i
    
    Dim SelItems As Variant
    ReDim SelItems(1 To lCount, 1 To 1)
     
     k = 1
     With ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                SelItems(k) = .List(i)
                k = k + 1
            End If
        Next i
    End With
    I'm sure I am making a simple error but I am unable to figure it out and it is driving me crazy! Any help would be much appreciated.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Populating an array with selected ListBox items

    You created a 2D array so you have to use:
    SelItems(k, 1) = .List(i)
    Everyone who confuses correlation and causation ends up dead.

+ 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