Listbox help

dave8

Active Member
Joined
Jul 8, 2007
Messages
275
I have setup a Listbox with the following properties: fmListSyleOption, fmMultiSelectMulti, ColumnCount=3. Here is the code:

Code:
For r = 3 To rowCount
        If Sheets("Sheet1").Range("B" & r).Value = ComboBox1.Value Then
                    
           ListBox2.List(i, 0) = Sheets("Sheet1").Range("B" & r).Value
           ListBox2.List(i, 1) = Sheets("Sheet1").Range("C" & r).Value
           ListBox2.List(i, 2) = Sheets("Sheet1").Range("F" & r).Value
           i = i + 1
         End If
    Next

I'm getting an error that says, "Invalid property array index", when it tries to execute the first Listbox2.List statement. What is wrong and what am I missing?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I have setup a Listbox with the following properties: fmListSyleOption, fmMultiSelectMulti, ColumnCount=3. Here is the code:

Code:
For r = 3 To rowCount
        If Sheets("Sheet1").Range("B" & r).Value = ComboBox1.Value Then
                    
           ListBox2.List(i, 0) = Sheets("Sheet1").Range("B" & r).Value
           ListBox2.List(i, 1) = Sheets("Sheet1").Range("C" & r).Value
           ListBox2.List(i, 2) = Sheets("Sheet1").Range("F" & r).Value
           i = i + 1
         End If
    Next

I'm getting an error that says, "Invalid property array index", when it tries to execute the first Listbox2.List statement. What is wrong and what am I missing?

Perhaps you have it somewhere else in your code, but I don't see an initial value for "i" in the snippet posted. If i Is Nothing then the code errors. I assume you want i = 0 for the initial value, if you do then it should be declared before the For statement.
 
Upvote 0
Is the .ColumnCount of the Listbox set to 3 or higher?

Also,

There might be issues if there aren't enough existing items in the List box.
To add those three lines to an empty ListBox (with .ColumnCount >=3)

Code:
For r = 3 To rowCount
        If Sheets("Sheet1").Range("B" & r).Value = ComboBox1.Value Then
                    
           ListBox2.AddItem Sheets("Sheet1").Range("B" & r).Value
           ListBox2.List(i, 1) = Sheets("Sheet1").Range("C" & r).Value
           ListBox2.List(i, 2) = Sheets("Sheet1").Range("F" & r).Value
           i = i + 1
         End If
    Next
 
Last edited:
Upvote 0
The ColumnCount property is set to 3, but I don't see the values from Sheet1.Range("B").value; I only see the values from "C" and "F" of Sheet1. In other words, the first column in the Listbox is blank.
 
Upvote 0
This works:

Code:
For r = 3 To rowCount
        If Sheets("Sheet1").Range("B" & r).Value = ComboBox1.Value Then
                    
           ListBox2.AddItem Sheets("Sheet1").Range("B" & r).Value

           ListBox2.List(i, 0) = Sheets("Sheet1").Range("B" & r).value
           ListBox2.List(i, 1) = Sheets("Sheet1").Range("C" & r).Value
           ListBox2.List(i, 2) = Sheets("Sheet1").Range("F" & r).Value
           i = i + 1
         End If
    Next
 
Upvote 0
Unless its used later in the routine, you don't need the argument i

Code:
For r = 3 To rowCount
    If Sheets("Sheet1").Range("B" & r).Value = ComboBox1.Value Then
        With ListBox2
            .AddItem Sheets("Sheet1").Range("B" & r).Value
            .List(.ListCount - 1, 1) = Sheets("Sheet1").Range("C" & r).Value
            .List(.ListCount - 1, 2) = Sheets("Sheet1").Range("F" & r).Value
        End With
    End If
Next
 
Upvote 0

Forum statistics

Threads
1,215,762
Messages
6,126,736
Members
449,334
Latest member
moses007

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