+ Reply to Thread
Results 1 to 18 of 18

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

Hybrid View

mcdermott2 listindex=0 when first item... 04-16-2013, 04:22 PM
coreytroy Re: listindex=0 when first... 04-16-2013, 04:34 PM
mcdermott2 Re: listindex=0 when first... 04-16-2013, 04:44 PM
JosephP Re: listindex=0 when first... 04-16-2013, 05:59 PM
mcdermott2 Re: listindex=0 when first... 04-17-2013, 10:02 AM
Norie Re: listindex=0 when first... 04-16-2013, 06:03 PM
mcdermott2 Re: listindex=0 when first... 04-17-2013, 09:59 AM
JosephP Re: listindex=0 when first... 04-16-2013, 07:55 PM
mcdermott2 Re: listindex=0 when first... 04-17-2013, 10:00 AM
Norie Re: listindex=0 when first... 04-17-2013, 10:11 AM
mcdermott2 Re: listindex=0 when first... 04-17-2013, 10:29 AM
mcdermott2 Re: listindex=0 when first... 04-17-2013, 10:32 AM
Norie Re: listindex=0 when first... 04-17-2013, 10:38 AM
mcdermott2 Re: listindex=0 when first... 04-17-2013, 10:42 AM
Norie Re: listindex=0 when first... 04-17-2013, 10:54 AM
mcdermott2 Re: listindex=0 when first... 04-17-2013, 11:03 AM
mcdermott2 Re: listindex=0 when first... 04-17-2013, 10:57 AM
mcdermott2 Re: listindex=0 when first... 04-17-2013, 12:38 PM
  1. #1
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

    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.


    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!

  2. #2
    Forum Contributor
    Join Date
    01-02-2007
    Location
    Australia NSW
    MS-Off Ver
    2013
    Posts
    494

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

    Does
    If Listbox3.Value = "" Then
    do the trick ?

  3. #3
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

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

    Nope. ListBox3.Value= Null for anything (or nothing) selected. Thanks for the idea though!

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

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

    the listindex starts at 0
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  5. #5
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

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

    Quote Originally Posted by JosephP View Post
    the listindex starts at 0
    Right, but doesn't the listindex=0 also if nothing is selected? How can I indicate in my code that I only want it run if something is selected

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

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

    How does the code not work if the top item is selected?

    Does it only not work when that's the only item selected?
    If posting code please use code tags, see here.

  7. #7
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

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

    Quote Originally Posted by Norie View Post
    How does the code not work if the top item is selected?

    Does it only not work when that's the only item selected?
    Yes, it only does not work if that is the only item selected. If I select the 2nd item in the list, or if I select the 1st item and other items the code runs. It seems the problem is that the listindex = 0 both when nothing is selected, and when just the first option is selected

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

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

    also if the listbox is multiselect the listindex can have a value >=0 even if no item is actually selected

  9. #9
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

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

    Quote Originally Posted by JosephP View Post
    also if the listbox is multiselect the listindex can have a value >=0 even if no item is actually selected
    Interesting. Do you have any ideas how I can differentiate between nothing selected, and anything selected?

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

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

    The index of a listbox is 0-based, ie it starts at 0.

    Usually when nothing is selected the ListIndex is -1 but you can't really use ListIndex reliably with a multiselect listbox, which is what I think you have.

    What exactly is happening when you only select the first item?

    Does the code error?

    Does the filter not filter correctly?

    Also, is it only the first item that has the problem? It doesn't happen when another item is the only one selected?

  11. #11
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

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

    Quote Originally Posted by Norie View Post
    The index of a listbox is 0-based, ie it starts at 0.

    Usually when nothing is selected the ListIndex is -1 but you can't really use ListIndex reliably with a multiselect listbox, which is what I think you have.

    What exactly is happening when you only select the first item?

    Does the code error?

    Does the filter not filter correctly?

    Also, is it only the first item that has the problem? It doesn't happen when another item is the only one selected?

    When the first item is selected the code does not error. I've walked through it step by step, and when just the first item is selected the listindex for listbox3 = 0. As such, the code works just as if nothing were selected, where the code skips everything within the parent "IF" statement.

    Also, yes, this is the only scenario where I have a problem. If I select the 2nd item, or any other item individually, or any combination of options the code runs.

  12. #12
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

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

    In case it is helpful, here is the code where I populate the listbox:

      
        Set country = CreateObject("Scripting.Dictionary")
        With country
            .CompareMode = 1
            For Each e In Sheets("Sheet1").Range("A2:A20000").Value
                If e <> "" Then .Item(e) = Empty
            Next
            Me.ListBox3.List = .keys
        End With

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

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

    So, what should happen when only the first item is selected?

    Perhaps you could upload a sample workbook so we can see what's going on?

    PS I think it might be something to do with Operator:=xlFilterValues which doesn't appear in the recorded code when you filter on one item.

  14. #14
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

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

    Quote Originally Posted by Norie View Post
    So, what should happen when only the first item is selected?

    Perhaps you could upload a sample workbook so we can see what's going on?

    PS I think it might be something to do with Operator:=xlFilterValues which doesn't appear in the recorded code when you filter on one item.
    If just the first item is selected, I would like the code to run. The "If ListBox3.ListIndex <> 0 Then" statement is my attempt to skip over the listbox if nothing is selected, and run the code if anything is selected. I will try to upload a workbook, but I have had trouble doing this in the past.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

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

    Don't check the ListIndex, as I said it's not reliable with a multiselect listbox.

    To see if nothing has been selected then check the length of the string MyArr after the loop.

    If it Len(MyArr) = 0 then no values have been selected, so you can exit the sub.

  16. #16
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

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

    Quote Originally Posted by Norie View Post
    Don't check the ListIndex, as I said it's not reliable with a multiselect listbox.

    To see if nothing has been selected then check the length of the string MyArr after the loop.

    If it Len(MyArr) = 0 then no values have been selected, so you can exit the sub.
    Thanks! Let me see if I can get this to work -- sounds like it should work perfectly. I'll post my results shortly

  17. #17
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

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

    I don't think I will be able to upload a sample. I get the following error: 403 [IOErrorEvent type="ioerror" bubbles=false cancelable=false eventPhase=2 text="Error2038"]

    sorry! I can try to explain my spreadsheet and userform if that might help

  18. #18
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

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

    Thanks, Norie! With your advice, I changed my code to the following:

        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
    If Len(MyArr) <> 0 Then
        MyArr = Left(MyArr, Len(MyArr) - 1)
        countrylist = MyArr
        MyArr = Split(MyArr, ",")
        Rows("1:1").AutoFilter _
            Field:=1, Criteria1:=MyArr, Operator:=xlFilterValues
    End If
    According to my testing, this works perfectly.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1