Quantcast
Viewing all articles
Browse latest Browse all 50163

listindex=0 when first item selected in a multiselect listbox on a userform?

Hello,

I have a multiselect list box that I only want code to run on if at least one option is highlighted. The below works for every scenario except if only the very top option listed in the listbox is selected. Is there another way I can pass over my code if nothing is selected? Is it strange that selecting the top option only returns a listindex = 0 ? It seems like it should be 1.


Code:

Private Sub CommandButton13_Click()


Dim l As Long
Dim m As Long
Dim countrylist As String


If ListBox3.ListIndex <> 0 Then
    Dim j As Long
    Dim MyArr As Variant
    For j = 0 To ListBox3.ListCount - 1
        If ListBox3.Selected(j) Then
            MyArr = MyArr & Me.ListBox3.List(j, 0) & ","
        End If
    Next j
    MyArr = Left(MyArr, Len(MyArr) - 1)
    countrylist = MyArr
    MyArr = Split(MyArr, ",")
    Rows("1:1").AutoFilter _
        Field:=1, Criteria1:=MyArr, Operator:=xlFilterValues
End If


Thanks!

Viewing all articles
Browse latest Browse all 50163

Trending Articles