+ Reply to Thread
Results 1 to 18 of 18

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

  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.


    Please Login or Register  to view this content.

    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
    Please Login or Register  to view this content.
    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 Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    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.

  6. #6
    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

  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 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?

  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
    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

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

    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:

    Please Login or Register  to view this content.

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

    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,643

    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?

    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

  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?

    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

  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:

    Please Login or Register  to view this content.
    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