Question : Getting One Row from a ListBox List

Is there any easy way to extract a single row of values from a multicolumn ListBox into an array?

Mike

Answer : Getting One Row from a ListBox List

Problem solved!!  You were right - the dimensions are different, the 2nd dimension has 10 elements.

It seems that if you don't provide an array to the ListBox, its .List array is automatically dimensioned to 9 (10 columns).
I guess when you use .AddItem, internally it performs a ReDim Preserve on the first dimension of the .List array in order to increase the row count.
But it starts and stays at 10 columns, whether you use them or not.

So the reason I was getting a Type Mismatch error is because columns 3 through 10 contained Nulls - and WorksheetFunction.Index can't process Nulls.

If you overwrite all the nulls by populating all ten columns (like this), Index works fine.

    Dim RowIndex As Integer, ColIndex As Integer
    For RowIndex = 0 To 9
        Me.ListBox1.AddItem Format(RowIndex, "00") & "-00"
        For ColIndex = 0 To 9
            Me.ListBox1.List(RowIndex, ColIndex) = Format(RowIndex, "00") & "-" & Format(ColIndex, "00")
        Next ColIndex
    Next RowIndex


So the solution is if you want to be able to use Index to get a row from a ListBox, you need fill and array first and assign it to the List - i.e. don't use .AddItem to populate the list.  Then it works fine.

Thanks again for your comments.

Mike


Random Solutions  
 
programming4us programming4us