In VBA UserForms, “focus” refers to the control that is currently active and ready to accept keyboard input. When a user opens a form, it is helpful if the cursor is already placed in the correct text box so they can start typing immediately without clicking.
VBA provides two main ways to actively control focus:
- .SetFocus Method: Programmatically forces the focus to move to a specific control.
- _Enter() Event: Detects when a specific control receives focus and executes a specific action.
This article explains how to use these two features to manage the behavior of your UserForm.
1. Setting Initial Focus (.SetFocus)
The .SetFocus method makes a specified control active. It is commonly used within the UserForm_Initialize event to place the cursor in a specific input field as soon as the form appears.
UserForm Code
' Event executed when the form is initialized
Private Sub UserForm_Initialize()
' Set initial focus to NameInput_TextBox
Me.NameInput_TextBox.SetFocus
End Sub
Explanation
By adding this code to the form’s module, NameInput_TextBox becomes active (the cursor blinks inside it) the moment the form is displayed. This allows the user to start typing their name immediately without having to click the box with the mouse.
Note: When using .SetFocus inside UserForm_Initialize, it may not work as expected if you try to focus on a control that does not have the lowest TabIndex (usually 0). To ensure reliability, it is often better to use the UserForm_Activate() event or simply adjust the TabIndex properties of your controls.
2. Executing Code When Focus is Received (_Enter Event)
The _Enter() event triggers the moment focus moves to a control, whether via the Tab key or a mouse click.
In this example, we display an input guide in a label (Status_Label) as soon as the user clicks into the Memo_TextBox.
UserForm Code
' Event executed when Memo_TextBox receives focus
Private Sub Memo_TextBox_Enter()
' Change the caption of Status_Label to show a guide
Me.Status_Label.Caption = "Please enter notes here (Max 500 characters)"
End Sub
' Event executed when Memo_TextBox loses focus
Private Sub Memo_TextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Clear the guide message
Me.Status_Label.Caption = ""
End Sub
Explanation
- Private Sub Memo_TextBox_Enter(): The moment
Memo_TextBoxgets focus, the text ofStatus_Labelis updated to show instructions to the user. - Private Sub Memo_TextBox_Exit(…): The
_Exit()event occurs when the control loses focus. We clear the guide message here so that irrelevant instructions do not remain on the screen when the user moves to another field.
Summary
In this article, we covered how to control UserForm focus using the .SetFocus method and the _Enter event.
- Use .SetFocus to force the cursor to a specific control (useful for initial form setup).
- Use the _Enter Event to detect when a control becomes active and provide context-sensitive help or actions.
Mastering these features allows you to build sophisticated user interfaces that guide user actions smoothly and help reduce input errors.
