PDA

View Full Version : Can't set ListBox



Autofreak
02-22-2006, 01:05 PM
Hello All,
can't figure out why VBA would not set my_Box to UserForm1.ListBox1 - "Type mismatch" tried diffrent ways, no result. Here I want to use the function below to return an item's index in ListBox1.

Thank you!
Serge


Sub getListBoxItem()

Dim my_form As UserForm
Dim my_Box As ListBox

Set my_form = UserForm1
Set my_Box = UserForm1.ListBox1
MsgBox GetItemIndex(my_Box, "account")

End Sub




Private Function GetItemIndex(ByVal lb As ListBox, ByVal item As String) As Integer
Dim i As Integer
item = LCase(item)
For i = 0 To lb.ListCount - 1
If LCase(lb.List(i)) = item Then
GetItemIndex = i
Exit Function
End If
Next
GetItemIndex = -1
End Function

Norie
02-22-2006, 01:59 PM
There are various different types of Listbox.

Where do you have this code? Excel/Word?

Does this work?


Dim my_Box As MSForms.ListBox

Autofreak
02-24-2006, 09:25 AM
Hi Norie,

Thanks, it worked for my ListBox in Excel. I also learned that it can be done like the following:
Dim my_Box As Object
set my_Box=UserForm1.ListBox1

Cheers,
Great Weekend for You,:beerchug:
Serge



There are various different types of Listbox.

Where do you have this code? Excel/Word?

Does this work?


Dim my_Box As MSForms.ListBox