Hide/Show modeless userform when activating/deactivating workbooks

J

Jeremy Gollehon

I''ve written the following code to make sure a modeless form hides if the
user activates another workbook while the form is showing. I'm posting to
see if there are better ways.

I dimension a public variable in a regular code module:
Public ShowUserForm1 As Boolean

In the ThisWorkbook object module I have:
-----------------------------------------------------------------------
Private Sub Workbook_Activate()
If ShowUserForm1 Then UserForm1.Show
End Sub

Private Sub Workbook_Deactivate()
If UserForm1.Visible = True Then
UserForm1.Hide
ShowUserForm1 = True
End If
End Sub
-----------------------------------------------------------------------


In the Terminate code of the Userform I have:
-----------------------------------------------------------------------
Private Sub UserForm_Terminate()
ShowUserForm1 = False
End Sub
-----------------------------------------------------------------------


This works great (as far as I've been able to test). Is there a better way?
Thanks!

-Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top