Consulting

Results 1 to 3 of 3

Thread: Can't set ListBox

  1. #1

    Can't set ListBox

    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

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    There are various different types of Listbox.

    Where do you have this code? Excel/Word?

    Does this work?
    Dim my_Box As MSForms.ListBox

  3. #3
    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,
    Serge


    Quote Originally Posted by Norie
    There are various different types of Listbox.

    Where do you have this code? Excel/Word?

    Does this work?
    Dim my_Box As MSForms.ListBox

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •