Hide close [X] button on excel vba userform for my progress bar
Below is a routine that you can call like this:
subRemoveCloseButton MyForm
or from within your form:
subRemoveCloseButton Me
Here's the code you'll need:
Private Const mcGWL_STYLE = (-16)Private Const mcWS_SYSMENU = &H80000'Windows API calls to handle windows#If VBA7 Then Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long#Else Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long#End If#If VBA7 Then Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long#Else Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long#End If#If VBA7 Then Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long#Else Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long#End IfPublic Sub subRemoveCloseButton(frm As Object) Dim lngStyle As Long Dim lngHWnd As Long lngHWnd = FindWindow(vbNullString, frm.Caption) lngStyle = GetWindowLong(lngHWnd, mcGWL_STYLE) If lngStyle And mcWS_SYSMENU > 0 Then SetWindowLong lngHWnd, mcGWL_STYLE, (lngStyle And Not mcWS_SYSMENU) End IfEnd Sub
You can work it out from the following snippets:
Select the cmdClose
buttonOn the Menu bar, choose View | Code
Where the cursor is flashing, enter the following code:
Private Sub cmdClose_Click() Unload MeEnd Sub
On the Menu bar, choose View | Object
, to return to the UserForm.
To allow users to close the form by pressing the Esc key:
Select the cmdClose buttonIn the Properties window, change the Cancel
property to True
To prevent users from closing the form by clicking the X button
When the UserForm
is opened, there is an X
at the top right. In addition to using the Close Form button, people will be able to close the form by using the X. If you want to prevent that, follow these steps.
Right-click on an empty part of the UserFormChoose View | Code
From the Procedure dropdown, at the top right, choose QueryClose
Where the cursor is flashing, paste the highlighted code from the following sample
Private Sub UserForm_QueryClose(Cancel As Integer, _ CloseMode As Integer) If CloseMode = vbFormControlMenu Then Cancel = True MsgBox "Please use the Close Form button!" End IfEnd Sub
On the Menu bar, choose View | Object
, to return to the UserForm.Now, if someone clicks the X
in the UserForm, they'll see your message.
This is an improvement of the above answer of @Peter Albert
- Windows API calls are now Office x64 safe
FindWindow
call was improved to find Excel UserForms only. The function in the original answer searches every window class (e.g. Explorer windows and other program's windows). Therefore it could happen that the [x] button of other programs or explorer windows have been removed when their name was the same name as the UserForm.
Private Const mcGWL_STYLE = (-16)Private Const mcWS_SYSMENU = &H80000'Windows API calls to handle windowsPrivate Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _ ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr #If Win64 Then Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongPtrA" ( _ ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr Private Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongPtrA" ( _ ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr#Else Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongA" ( _ ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr Private Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongA" ( _ ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr#End IfPublic Sub RemoveCloseButton(objForm As Object) Dim lngStyle As LongPtr Dim lngHWnd As LongPtr Dim lpClassName As String lpClassName = vbNullString If Val(Application.Version) >= 9 Then lpClassName = "ThunderDFrame" Else lpClassName = "ThunderXFrame" End If lngHWnd = FindWindow(lpClassName, objForm.Caption) lngStyle = GetWindowLongPtr(lngHWnd, mcGWL_STYLE) If lngStyle And mcWS_SYSMENU > 0 Then SetWindowLongPtr lngHWnd, mcGWL_STYLE, (lngStyle And Not mcWS_SYSMENU) End IfEnd Sub
ThunderDFrame?
The UserForms in Excel are actually of the Windows classThunderDFrame
, which is the class for all UserFroms in Microsoft Office applications after 2002. Before that, it wasThunderXFrame
.