+ Reply to Thread
Results 1 to 10 of 10

Identification of selected items in multi-user List Box to call different macros

  1. #1
    Registered User
    Join Date
    10-19-2012
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    16

    Identification of selected items in multi-user List Box to call different macros

    Hello,
    I have scoured the internet for a solution to my problem but can’t seem to find a post that completely addresses my issue.
    I have a multi-select listbox which contains five items.
    • If Item 1 is selected, I’d like to call ‘Sub A()’.
    • If Items 2 and/or 3 and/or 4 are selected, I’d like to call ‘Sub B()’.
    • If Item 1 and either of items 2, 3, and/or 4 are selected, I’d like to call ‘Sub C()’.
    • If Item 5 is selected, I’d like to call ‘Sub D().’
    Many thanks in advance for your time, expertise and willingness to help a stranger.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Identification of selected items in multi-user List Box to call different macros

    Try this approach
    Please Login or Register  to view this content.
    *Note that List has a 0-based index
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    10-19-2012
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Identification of selected items in multi-user List Box to call different macros

    MANY THANKS! Your code is much more simple than anything I was trying to write.
    Now that I’ve got the list box working I have realized that there is a corollary to my question. That is, how can I make items 1-4 mutually exclusive of item 5? Item 5 is ‘None of the above’ so if that is checked, items 1-4, if previously selected, should be un-selected. Can you give me an idea of how to do that?
    Again, your help is most welcome and appreciated.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Identification of selected items in multi-user List Box to call different macros

    I don't have time to test it but try this. If there is any change to the selections, and item 5 is one of the selections, then it will deselect 1-4. That is, if you click 5 then the others are cleared. Then it will not let you select anything else as long as 5 is still selected.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-19-2012
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Identification of selected items in multi-user List Box to call different macros

    Thank you once again. Your above code makes sense to me and looks like it would work for what I’m trying to do. However, after working on this for two days I can’t get it to work. Here’s my actual code (My list box is named lstAFibRisks.):
    Please Login or Register  to view this content.
    As examples . . .
    If item 0 is selected, the code will run to the ELSE statement but then will go to the very top and start over to run through again and never makes it to the END IF statement.
    If item 5 is selected, the code runs to the fifth line but doesn’t advance to the line (Next i) to continue the loop. Instead it goes to the very top and starts over to run through only the first three lines of the IF statement.
    Do you have any thoughts as to why this happens? I am very grateful for your help.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Identification of selected items in multi-user List Box to call different macros

    Oh, I'm sorry. The act of changing the selection is a change, causing this Sub to be called again. We have to prevent that. Try this. If this still doesn't work then it would help if you attach your file.
    Please Login or Register  to view this content.
    By the way your Else part doesn't make sense logically. What that says is if item 5 is not selected, then set item 5 to not selected. It's redundant.

  7. #7
    Registered User
    Join Date
    10-19-2012
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Identification of selected items in multi-user List Box to call different macros

    Still having troubles . . . the routine exits at the first line (If Not EnableEvents Then Exit Sub). I’ve attached my file. The bit of code of interest is at the top of the UserForm module (I’ve bookmarked it).
    Attached Files Attached Files

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Identification of selected items in multi-user List Box to call different macros

    OK, my mistake. I did not test this because I didn't have your file and didn't take the time to make one up from scratch.

    Now that I see what you are doing everything makes a lot more sense. Please try the attached. Handling "None of the above" is tricky, because behavior depends on what was selected to start with.

    If user selects any of 1-4, then subsequently selects 5, then 1-4 should deselect.
    If user selects 5, then subsequently selects any of 1-4, 5 should deselect.

    The problem is that you can't tell using built-in functions what order they selected those items, only that "something changed." So I've added a variable to keep track of when they select "None of the above." I am pretty sure this gives you the behavior you want.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    10-19-2012
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Identification of selected items in multi-user List Box to call different macros

    This is PERFECT! Exactly what I was trying to accomplish but never would have been able to without your help. Please know that I am most grateful for your help (my boss, too). You are a gem. I would not have come up with that bit of code on my own. Know that I will pay your kindness forward.

  10. #10
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Identification of selected items in multi-user List Box to call different macros

    I'm glad it helped! Feel free to scan the forum for questions you can answer.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How to pass user selected list items from listbox to another procedure in excel using vba
    By mahendra.asapu in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-20-2016, 03:51 PM
  2. [SOLVED] Caption Multi Selected Items from Listbox to Label (One in Each Line)
    By boss1982 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-09-2014, 09:12 AM
  3. actionbased multi selected items in listbox
    By mido609 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-15-2013, 11:42 AM
  4. Replies: 1
    Last Post: 09-03-2012, 10:03 PM
  5. [SOLVED] Find Multi Selected Items in Listbox in Range
    By Sniper in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-08-2012, 07:53 AM
  6. multi select listbox, remove selected items.
    By D_Rennie in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-25-2009, 08:39 AM
  7. Replies: 1
    Last Post: 06-24-2005, 12:21 AM

Tags for this Thread

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