Set focus on vba userform after listbox selection?

smeldy

New Member
Joined
Feb 1, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

I've been running into a rather inconsistent and frustrating problem for the past couple of days while on my userform.
My userform consists of a series of listboxes that filter a table depending on the user's selection in the listbox.
The problem is that sometimes after selecting an item in one listbox, the entire userform appears to lose focus such that the user can't select items in other listboxes unless I right-click the userform.

My userform is modeless, I've tried sending right clicks via send keys, tried activating the workbook containing the macro hoping that that solves the problem, and have tried some APIs from the windows library
but these don't seem to work. The most frustrating part is that this appears to be rather inconsistent where sometimes I'm able to select other listboxes and other times I can't. The only consistent way of fixing this problem is if the user right clicks the userform but this gets annoying.

If this is potentially due to the events that occur upon listbox selection, I'm processing the listbox item selection in the following manner: Item selected => mousedown event is called. If listbox is single select, then run mouseup event else run listbox change event occurs if the listbox is multiselect.

Would attach the macro itself if it wasn't for the fact that it's a bit lengthy and catered specifically to sensitive data from a table.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi and welcome to the board,

Does part of your macro consist of leaving the userform and selecting a range(s) in a worksheet or activating an item in the application user interface?
 
Upvote 0
Part of my macro does some copy and pasting to a temporary sheet so I can convert filtered non-contiguous rows to a range. If it helps, when the userform appears to lose focus, it does seem as if the second workbook I'm using (to access the table of data) is activated. Going into developer / the second workbook does also fix the lack of focus but there are also times where the macro-containing workbook appears to be active but the userform isn't in focus.

I don't set focus to any element on the userform (not that setting focus to listboxes or other things on the userform fixes the issue)
 
Upvote 0
As expected, it seems that when your code performs the copying and pasting, it un-selects the modeless userform.

Not knowing your code, it is difficult to find a workaround ... But the idea is to have some code at the end of each macro so that when the macro finishes copying, pasting, selecting .. etc, the code would then set the focus back to the userform.
 
Upvote 0
Yeah I suspect it's something like that...
How would I go about setting the focus back to the userform? I've tried setting focus to controls on the userform but it doesn't fix anything nor does activating the macro-containing workbook.

I'll try and see if I can maybe cut an excerpt of my code when I am able to. There's just a lot of code that might not really make sense without the entire userform.
 
Upvote 0

Forum statistics

Threads
1,213,567
Messages
6,114,344
Members
448,570
Latest member
rik81h

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top