+ Reply to Thread
Results 1 to 3 of 3

VBA Listbox - Mysterious selection after change event

  1. #1
    Registered User
    Join Date
    03-23-2020
    Location
    Toronto,Canada
    MS-Off Ver
    2019
    Posts
    2

    VBA Listbox - Mysterious selection after change event

    Hi All,

    I apologize in advance as this is quite long winded, but not the most straightforward code as a lot has to happen at once. I have been working on some code for the quick selection of a variety of options in different categories. I started to run into a very frustrating problem with ListBoxes where there seems to be some sort of click or selection event that occurs after I click an item, which is calling other routines and initializing code it shouldn't be.

    To be more precise, I have attached a sample workbook with one of the Userforms that I'm struggling with. In this Userform, each listbox is populated with values under that specific category from a list in an excel worksheet. The first Listbox shows items that have a common flag in the worksheet, and the last Listbox is meant to store all the selected items. Now my intention is that when a user clicks an item, it disappears from that ListBox (and the common listbox if it's also present there) and appears in the summary listbox. To make matters more complicated, each listbox has a search bar above it, and every time text is entered in the by the user, the listbox changes to only show remaining items that are 'Like' what is typed in. Because of this and the fact that users may want to search for multiple items, the items are all stored in a public array on initialization after being populated to be able to repopulate. This array also contains a flag to know if they've been selected so that they don't re-appear after text is altered in the search box during repopulation. If, however, the item is subsequently removed from the summary listbox, the selected flag is removed and it reappears in the original listbox (I opted to forgo adding it back into the common box after it has been removed).

    So hopefully you have an idea of the logic in my code. Anyways, I think all the logic is sound at this point, but I run into this inconsistently consistent problem of what I'll call the 'ghost clicks'. If I click an item in a listbox, it becomes selected and triggers the listbox_change event as expected. This event runs a function which flags the item as selected in the corresponding array, removes it from that listbox +/- the common listbox, and adds to it to the summary listbox. Now in most cases this works perfectly fine, but I get a reproducible error of having not only the item I clicked moving to the summary box, but also the item that will take its position in the box after the intended item has been removed.

    Now I know what some of you may be thinking and I found a similar issue in this forum of a 'click through' event, where the original click is still acting once the items have shifted. This was solved with a delay in the listbox to prevent multiple clicks in less than 0.2 seconds. Well I tried that, and even tried 2.2 seconds, but that second 'ghost click' still seems to find its way through. I've tried pauses in the code in case the deselection of the listbox item is taking longer than the code that removes the item (if that is even possible). I've also stepped through the code line by line to try and find what could be triggering this. I even reset the listbox listindex to -1 at the end of my change event but once again, it still finds a way to become selected, run my change event, and move itself over. When I step through line by line, it seems the item that will replace the position of the item that disappeared gets selected AFTER the end sub line of the change event (ie when no code should be running), which re-triggers that event. I thought of putting a timer to prevent the event from re-triggering before a certain time, but that still wouldn't explain why the item is being selected on its own in the first place.

    Interestingly, I also added a button that seems out of place, at the top of the userform, which selects an item without me having to physically click it. This seems to prevent the issue as far as I can tell, so I'm guessing it has something to do with the physical click itself (which in my mind should have been corrected by the delay). The 'ghost click' doesn't happen all the time, but I find that during a given session, when I reproduce the same sequence of events in the userform, I consistently can get the 'ghost click' to happen regardless of how short or long I click.

    I have spent way too many hours on this so any enlightenment would be greatly appreciated.
    Attached Files Attached Files
    Last edited by MDCode; 03-23-2020 at 04:22 PM.

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: VBA Listbox - Mysterious selection after change event

    I played around with your UserForm and sure enough I could get it to act strange every time. Just hold down the mouse button and then move the mouse over any of the listboxes. Not sure why or how this is happening. My best suggestion is to change your "trigger" code to use the _MouseUp() event instead as that would prevent the extra events from firing I believe. I tried to do this in the attached.

    WBD
    Attached Files Attached Files
    Office 365 on Windows 11, looking for rep!

  3. #3
    Registered User
    Join Date
    03-23-2020
    Location
    Toronto,Canada
    MS-Off Ver
    2019
    Posts
    2

    Re: VBA Listbox - Mysterious selection after change event

    Thanks WBD! as far as I can tell, that does seem to be preventing that extra click/selection/whatever else is going on. It's still a big mystery to me why the click event wouldn't work based on the behaviour I was seeing. I'm not sure if it's something implicit about how the click event is run, but as I mentioned, even when I step into the code line by line, the mouse is well away and the click has finished long ago, yet it's as if the click queued another click which waits until the majority of the other code has finished to fire.

    Now for this particular form, this works great I believe. One issue I can foresee, however, is other forms that I have that are similar. In these, there is no summary box, and instead of items being removed from the box when clicked, the box is set to multiselect so that multiple items can be selected. Now because I still have a common listbox with duplicate entries, I need the selection of an item, via code, to activate some sort of event that 'triggers' when the item is either clicked manually or the selection property is changed to 1 in the code. It is my understanding that the mouse up event wouldn't fire in the case of a coded selection change, thus I would still need some way of having this block of code fire without the change event. Again, the logic to this code seems to be working, so ideally it would be nice not to have to re-work through new logic to make it work. That being said, if I can't find out why this is happening and stop it, that may be my only choice.

    What I'm finding tricky with all my code is all the search boxes I added. Because they have to completely clear and repopulate the listbox every time they change, I'm relying on the arrays to be updated for any change event whatsoever, to capture selections.

    Regardless, I appreciate the time and the fix. I can now at least say that this form is usable.

    MDCode

+ 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. [SOLVED] DblClick event on listbox crashes Excel when I move the listbox as a result of the event
    By feanturi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-22-2022, 11:38 AM
  2. Populate (ca 200) Txtboxes depending on listbox selection, live-filter for listbox &1 more
    By InternInNeed in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-14-2016, 09:56 AM
  3. [SOLVED] listbox change event won't trigger when listbox is updated
    By Highlander777 in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 06-01-2013, 09:03 AM
  4. Editing listbox items in Dblclick event freezes Excel unless mouse moves over listbox
    By muneebmansoor in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-28-2013, 02:21 PM
  5. [SOLVED] Indexing proper range in listbox depending on selection in previous listbox
    By bloodmeat in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2013, 02:31 PM
  6. Replies: 12
    Last Post: 08-28-2012, 07:09 AM
  7. Multi select Listbox Items selection based on other Listbox item selection.
    By srinivassathi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-20-2011, 05:53 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