Pls help me understand dynamic array

dave8

Active Member
Joined
Jul 8, 2007
Messages
275
I have a listbox on a form set as MultiSelect and I want to put the rows that were selected in an array. Consider this code:

Code:
Dim chkBoxArr() as long
Dim icount, j as integer

For j = 1 To Userform1.Listbox1.ListCount - 1
     If UserForm1.ListBox1.Selected(j) Then
        chkBoxArr(icount) = j
        icount = icount + 1
     End If
  Next j
 
 ReDim Preserve chkBoxArr(j)
 
 For icount = LBound(chkBoxArr) To UBound(chkBoxArr)
    If chkBoxArr(icount) <> 0 Then
      MsgBox chkBoxArr(icount)
  Next icount


I want to put the row number of the check box selected from the listbox into an array. For example, if I select row 2 from the listbox, the number 2 gets put into the array cell; if row 10 gets selected, then the number 10 gets put into the array cell. I can select multiple rows from the listbox and have these row numbers in the array (2, 10, 15, 19, etc). I created a dynamic array because the listbox items can be over several hundred and I only want to display the row numbers in the array.

The problem is that I can't display the array elements. Are these values getting into the array or are they being overwritten?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try this:

Code:
Sub x()
    Dim aiSel()     As Long
    Dim iSel        As Long
    Dim iList       As Long
 
    With UserForm1.ListBox1
        ReDim aiSel(1 To .ListCount)
 
        For iList = 0 To .ListCount - 1
            If .Selected(iList) Then
                iSel = iSel + 1
                aiSel(iSel) = iList
            End If
        Next iList
 
        ReDim Preserve aiSel(1 To iSel)
 
        For iSel = 1 To iSel
            MsgBox aiSel(iSel)
        Next iSel
    End With
End Sub
 
Upvote 0
Thank you. But, I don't understand why have to ReDim at the beginning? I haven't put anything in it yet?
 
Upvote 0
It doesn't matter that you've put nothing in it.

In fact unless you redim it at the start you wouldn't be able to put anything in it.
 
Upvote 0
Until the array is dimensioned, there is no storage allocated, no there's no place to store anything.
 
Upvote 0
Code:
Dim rowsMatch as Variant
Dim i as Long

rowsMatch = vbNullString

With ListBox1
    For i = 0 to .ListCount - 1
        if .Selected(i) then rowsMatch = "," & i
    Next i
End With

rowsMatch = Split(Mid(rowsMatch,2), ",")

For i = 0 to UBound(rowsMatch)
    MsgBox rowsMatch(i)
Next i
 
Upvote 0
SHG, you helped me with an array routine and now I want to experiment with some changes. I want to be able to pass the array to a procedure and read from it. I tried it but I just get the first element in the array. How do I pass the array (in the calling procedure) and read all the elements in the called procedure.
 
Upvote 0

Forum statistics

Threads
1,215,467
Messages
6,124,984
Members
449,201
Latest member
Lunzwe73

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top