Events
The event handler is pre-determined by the name of the control.
Event handlers procedures must be placed in the corresponding userform module.
| Activate | Occurs when the userform is displayed or reactivated. This event will only fire: if the userform is visible. (A userform loaded with the Load statement is not visible until you use the Show method) and when you switch between userforms. This event will NOT fire: when you switch between the application and a modeless userform. |
| AddControl | Occurs when a control is added to the userform at run-time. |
| BeforeDragOver | Occurs when a drag and drop operation is in progress while the mouse pointer is over the userform. |
| BeforeDropOrPaste | Occurs when the left mouse button is released and data is dropped and pasted ? |
| Click | Occurs when the left mouse button is clicked when over the userform. This does not fire when you click on a control on the userform. |
| DblClick | Occurs when the left mouse button is double clicked when over the userform. |
| Deactivate | Occurs when the userform loses focus because a subform is displayed. |
| Initialize | Occurs when the userform is loaded into memory, this is before the userform is displayed. |
| KeyDown | Occurs when any key is pressed while the userform is displayed. |
| KeyPress | Occurs when an ANSI key is presseed while the userform is displayed. |
| KeyUp | Occurs when any key is released while the userform is displayed. |
| Layout | Occurs when the size of a userform is changed at run-time. |
| MouseDown | Occurs when a mouse button is pressed ?? |
| MouseMove | Occurs when the mouse pointer is moved over the userform at run time. |
| MouseUp | |
| QueryClose | Occurs before a userform is closed or unloaded. |
| RemoveControl | Occurs when a control is removed from the userform at run-time. |
| Resize | Occurs when a userform is resized |
| Scroll | Occurs when the userform is scrolled ? |
| Terminate | Occurs when a userform is removed from memory as part of the clean up task |
| Zoom | Occurs when the userform is zoomed ??? |
Initialize Event
This event is fired when a userform object is created
The Initialise event is typically used to prepare the userform. Controls can be populated and given values, even moved or resize.
The Initialise event fires after the userform is loaded, ie when a new userform object is created
This is always the first event that a userform calls
Dim oForm As Userform1
Set oForm = New Userform
Sub Userform_Initialise
End Sub
QueryClose Event
It is not possible to remove the "x" from the top right corner although you can disable it.
Whether you display a prompt to the user or not is up to you.
Using the QueryClose event allows you to intercept the message to close the userform before it is actually closed.
If you set the Cancel paramater to True, then the userform will not be closed.
This means the userform has been closed by the user pressing the top right x.
Private Sub UserForm_QueryClose(Cancel as integer, CloseMode as integer)
If CloseMode = vbQueryClose.vbFormControlMenu Then
cmdCancel.SetFocus
MsgBox "Cannot close the userform !!"
Cancel = True
End If
End Sub
This means the userform has been closed by code
If CloseMode = vbQueryClose.vbFormCode Then
Cancel = True
End If
Removing the Close button
'userform window
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'current window state
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
'new window style
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const GWL_STYLE = -16
Const WS_SYSMENU = &H80000
Public Sub RemoveCloseButton(oUserForm As MSForms.UserForm)
Dim hwnd As Long
Dim lStyle As Long
hwnd = FindWindow("ThunderXFrame"), oUserForm.Caption)
lStyle = GetWindowLong(hwnd, GWL_STYE)
SetWindowLong hwnd, GWL_STYLE, (lStyle And Not WS_SYSMENU)
End Sub
Activate Event
This event is fired every time the userform becomes the active window
The Activate and Deactivate events only occur when the focus is moved within the application.
Moving the focus to another application will not trigger these events.
The Activate event fires after the Show method is activated
Private Form_Activate()
End Sub
Terminate Event
The terminate event is not triggered if your application executes the End statement while the userform is loaded.
Private Form_Terminate()
End Sub
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext