+ Reply to Thread
Results 1 to 27 of 27

Deselect all in multi-select listbox upon opening

  1. #1
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Deselect all in multi-select listbox upon opening

    For groups of checkboxes I know a fairly easy way of turning off all selections upon opening.
    But with that you target the checkboxes, which have a certain name or all checkboxes in general.

    The options in a listbox don't really have their own names that you can target in VBA (as far as I am aware).

    So what code would I need to turn off all selections made in List Box 1, 2&3?

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Deselect all in multi-select listbox upon opening

    Hi,

    There shouldn't be anything selected in a listbox on first opening so I'm not sure why you need code?
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Re: Deselect all in multi-select listbox upon opening

    Erm... I just opened my file that I saved with a few options selected and when I close it and open it again, it still has those options selected...
    I want to be able to start with a clean slate every time I open it.
    Because one of the listboxes has more options that can fit on a screen, so scrolling down every time to check if everything is deselected to start with is something I would rather not have to do.

    But I also would like an option for users to be able to select or deselect all of a particular listbox... Which I was hoping to be able to derive from this first option :P
    Last edited by ruub7; 06-08-2017 at 03:26 AM.

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Deselect all in multi-select listbox upon opening

    Is the listbox on a worksheet, or a userform?

  5. #5
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Re: Deselect all in multi-select listbox upon opening

    On a worksheet

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Deselect all in multi-select listbox upon opening

    That is strange- I cannot reproduce that in 2010. All selections are gone upon reopening the workbook.

  7. #7
    Registered User
    Join Date
    03-07-2017
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    182

    Re: Deselect all in multi-select listbox upon opening

    It also happened with my previous project, What I did I add a clear checkbox button.

    I add this code
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Re: Deselect all in multi-select listbox upon opening

    Unfortunately, that doesn't work, because they're form control listboxes, not activeX...

    (I am a noob to VBA, but it seems that you have written a code that sets all checkboxes to false, I want a code that deselects all options from a listbox, wether that is controlled by a checkbox or a button doesn't really matter to me)
    Last edited by ruub7; 06-08-2017 at 08:57 AM.

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Deselect all in multi-select listbox upon opening

    Aha- that explains it. You can use this to deselect all items in a form listbox
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Re: Deselect all in multi-select listbox upon opening

    This gives a really strange result, it somehow only works for the last listbox, the other listboxes still keep their selections upon opening.

    (I have added a tiny bit to your code to get it to trigger upon opening and a specific sheet, but that shouldn't interfere with your code to clear the listbox selections...)

    Please Login or Register  to view this content.

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Deselect all in multi-select listbox upon opening

    If there is more than one listbox, you need to loop through them all
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Re: Deselect all in multi-select listbox upon opening

    Ah, the noobishness from me surfaces again
    Thnx

    But the thing is, with your initial code, wether I change the n=1 to for example n=4, it still clears the selections from the same listbox...

    So what do I need to change to get it to target different listboxes? (different listbox numbers)

  13. #13
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Deselect all in multi-select listbox upon opening

    The n variable is for looping through the items in a listbox, not for determining which listbox. The code I posted a minute ago will loop through all listboxes on that sheet.

  14. #14
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Re: Deselect all in multi-select listbox upon opening

    yup, I know and that one works like a charm now, thnx for that.

    but what do I need to change to target a specific listbox, for example to select/deselect all items in 1 listbox only, by pressing a simple button?

  15. #15
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Deselect all in multi-select listbox upon opening

    Refer to it by name or number
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Re: Deselect all in multi-select listbox upon opening

    Hmmz, yes, your code works if I only want to be able to set everything to false.

    But I would like to be able to set it either all false or all true based on a checkbox.

    I though this would make it work:

    Please Login or Register  to view this content.
    But it doesn't, because now it sets everything to true, both when you check and when you uncheck the checkbox that is linked to this macro...

    What am I doing wrong?
    Last edited by ruub7; 06-09-2017 at 07:46 AM.

  17. #17
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Deselect all in multi-select listbox upon opening

    A Form checkbox actually returns xlOn (1) or xlOff (-4146). Any non 0 number will implicitly convert to True as a boolean, so you need to use something like this
    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Re: Deselect all in multi-select listbox upon opening

    Thnx, really usefull to know that for the future.

    And it all works like a charm now!

  19. #19
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Re: Deselect all in multi-select listbox upon opening

    Crap, now a problem has surfaced with a different part of my code.

    I have a part of a code that copies the selected items from the listboxes to another sheet which I use to apply the filter, but the checkbox that can now select/unselect everything visually, does not translate to all items being added/deleted from the filter sheet...

    This is the code I have for the to copy the selected items to the filter:

    Please Login or Register  to view this content.

  20. #20
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Deselect all in multi-select listbox upon opening

    I would recommend that you move the processing code to a separate routine which you may then call from both controls
    Please Login or Register  to view this content.
    You will then require a separate macro for the listbox itself
    Please Login or Register  to view this content.
    and a similar routine for the checkbox but it must specify the name of the listbox rather than using Application.Caller

  21. #21
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Re: Deselect all in multi-select listbox upon opening

    Erm... I have linked the listboxes to the seperate listbox_click macro, but now the filter doesn't update at all anymore...

  22. #22
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Deselect all in multi-select listbox upon opening

    As I cannot see exactly what you have done, I cannot really comment as to why it would not be working. Perhaps you could provide the workbook?

  23. #23
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Re: Deselect all in multi-select listbox upon opening

    I had to do some serious deleting and editing to be able to provide a workbook, so I really hope I didn't delete too much.

    But I think this should be enough for you to figure out what is wrong.
    Last edited by ruub7; 06-12-2017 at 06:55 AM.

  24. #24
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Deselect all in multi-select listbox upon opening

    Your listbox name is not Listbox 1, Listbox 2 or Listbox 3, so the ListBox_Select routine doesn't do anything with the array it creates. Did you rename the control?

  25. #25
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Re: Deselect all in multi-select listbox upon opening

    That is odd, previously I had this problem, but then the exact opposite.

    I had renamed the listboxes, but the the code still needed to be List Box 1, 2 etc. in order to make it work.
    And now somehow it does accept the names that I gave them...
    Oh well, it works now.


    And how do I get the checkboxes to work?
    Do I need to make a seperate sub for each of the checkboxes, to call the select all function for that specific checkbox/listbox combo + the auto update thing of the filter?
    Or is there a simpler way to achieve that for multiple checkboxes that control different listboxes?

  26. #26
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Deselect all in multi-select listbox upon opening

    If you use a naming convention for the checkboxes that would allow your code to identify the listbox to control, then you could use the same macro for all of them.

  27. #27
    Registered User
    Join Date
    05-08-2017
    Location
    bla
    MS-Off Ver
    2010/2013
    Posts
    62

    Re: Deselect all in multi-select listbox upon opening

    Everything seems to be working properly now.
    Thnx for all your help!

+ 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. Multi-Select Listbox Help!
    By gclode in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-05-2016, 11:14 AM
  2. [SOLVED] Unhide 2nd Listbox when condition is met in the 1st multi-select listbox
    By pjbassdc in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-18-2015, 09:58 AM
  3. [SOLVED] ListBox Multi-Select - Help
    By KyrieInfinity in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-08-2013, 12:54 AM
  4. Multi-select listbox output.
    By leaning in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-20-2011, 02:06 PM
  5. Multi-Select ListBox
    By yawnzzzz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-17-2011, 08:55 AM
  6. Listbox Multi Select
    By MikeT in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2006, 03:30 PM

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