I want to input data in TextBox2 for searching data in ListBox1, but it has error "Could not set the List property. Invalid property value". Debug on ".List(iii, ii - 1) = x1(i, ii)"
My code
Private Sub TextBox2_Change()
Dim x1, i As Long, ii As Long, iii As Integer
x1 = [myCar]
Application.ScreenUpdating = False
With ListBox1
If TextBox2 = "" Then
.RowSource = "myCar"
Else
.RowSource = ""
For i = 1 To UBound(x1, 1)
If LCase(x1(i, 3)) Like LCase(TextBox2) & "*" Then
For ii = 1 To 12
.AddItem
.List(iii, ii - 1) = x1(i, ii)
Next
iii = iii + 1
End If
Next
End If
End With
End Sub
The variable x1
isn't declared. Therefore it should be a variant. It seems that [myCar]
is meant to be a range name. If so, it might be possible to assign it to x1
if its location were known. Perhaps your TextBox is on a worksheet and the reference actually works. Perhaps x1
is empty from that point forward. I suggest you test.
Next you assign "MyCar" to the ListBox's RowSource property. As you know, that property holds a string. If MyCar
is a rangename it can't be a valid range address. Perhaps something like Range(MyCar).Address
would work. .RowSource = "myCar"
simply assigns the word "MyCar" to the RowSource, and that should fail.
In For i = 1 To UBound(x1, 1)
you now expect x1
to be an array. Perhaps x1 = [myCar]
is equivalent to ActiveSheet.Range(MyCar).Value
. I wouldn't use that syntax but you have checked its functionality above.
Presuming that x1
actually holds the values of a range. assigning them to the List property of the ListBox should fail if the ColumnsCount property isn't set. That isn't apparent from your code. Neither can we tell if the columns available in x1 are equal to those available in the list box or those demanded by the code.
The ListBox has rows and columns. Given that your variable i counts the rows the your variable ii should count the columns (I would use R and C to help me follow what I'm doing). Then, with rows and columns all accounted for, what does your variable iii count?
So, it is definite that there is insufficient control of the rows and columns of the ListBox in your code. However, after that has been sorted you may find that the values you wish to assign don't exist. Therefore the line that crashes your program may contain more than one mistake.