Bring focus back to a Modeless UserForm - Solved

alhypo

New Member
Joined
Jul 2, 2012
Messages
19
Note: An idea of how to fix this occured to me after I typed this up, so this issue is solved for me. I decided to post it anyway since it might be useful to someone else. The solution is at the bottom of this post.

Greetings,

This is what I think should be a simple problem but I can't seem to figure it out.

Basically I have an Excel VBA project with a spreadsheet dashboard. The dashboard has a few embedded buttons the user can activate. One button brings up a modeless user form which allows data to be entered. I want the form modeless so they can still access other spreadsheet which might contain data they need or if they just happen to be working on multiple projects, I don't want to force them to finish the form before switching to another task.

In order to prevent the user from accessing the dashboard while the form is active, I do this:

Code:
Private Sub UserForm_Activate()
    Set tWB = ThisWorkbook
    Windows(tWB.Name).Visible = False

Then, when the form finishes:

Code:
Private Sub UserForm_Terminate()   
    Windows(tWB.Name).Visible = True

This works quite well. The userform remains visible but the spreadsheet disappears until the form is dimissed.

The problem is, hiding the workbook in this manner seems to automatically send focus to another open workbook. Normally, that makes sense of course, because you don't want focus to remain on a non-visible workbook. I would, however, like focus to remain on the UserForm. But I can't figure out how to get focus back to the form.

Solution:

So the problem was that I was hiding the workbook in the UserForm_Activate() event. This event occurs after the form has appeared. So the form appears, then the workbook is hidden, which causes focus to shift to the next available workbook. However, the UserForm_Initialize() event happens before the UserForm actually appears. Hidding the workbook in the initialize event will still shift focus to the next available workbook, however, the UserForm appears after that event so focus will come back to the form when it appears.

New Code:

Code:
Private Sub UserForm_Initialize()
    Set tWB = ThisWorkbook
    
    Windows(tWB.Name).Visible = False
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,215,353
Messages
6,124,462
Members
449,163
Latest member
kshealy

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