Focus Event

N

Nigel

I have a userform that is loaded as modeless, so that the user can interact
with the underlying worksheet.

Question: Is there an event I can use / create that allows my code to detect
when the userform has focus?

Thanks
 
G

Guest

doubleclick on userform and insert

Private Sub UserForm_Click()
MsgBox ("Got focus")
End Sub


"Nigel" skrev:
 
N

Nigel

Thanks for that, I understand that if the user clicks the userform I can
determine if the userform has focus.
What I need to be able to do is test, in code, if the userform has focus or
not - before the user clicks the userform.
 
G

Guest

There really is nothing to tell you that the user form has focus. The click
event is close but if the user clicks on a button on the form instead of the
form itself then the userform gets focus without the userform_click event
firing. So basically you are on your own to come up with some sort of a
creative solution around your problem.
 
Joined
Apr 18, 2011
Messages
1
Reaction score
0
I have a creative solution. To find out if your UserForm has Lost Focus you can’t use Private Sub CommandButton1_LostFocus() because that only works for imbedded ActiveX controls, not UserForms. If you knew that the Active Cell had changed, that would mean that the UserForm had Lost Focus – because the User must have clicked some other cell and left the UserForm. If you had access to the Sheet maybe you could use Worksheet_SelectionChange(). In my case I was working with an Add-In so the Sheet was not accessible. I had success by seeing if ActiveCell.Address had changed via:

Dim CellAddress As String
Private Sub UserForm_Activate()
CellAddress = ActiveCell.Address
End Sub
<new sub>
If CellAddress <> ActiveCell.Address
Then <the UserForm has Lost Focus>

 
Last edited:

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