Disabling a userform's "X" button?

6yxx

New Member
Joined
Oct 7, 2007
Messages
47
just a quick question, is it possible to disable a vb userform's terminate (X) button at the top right?
if so, how can it be done?

thanks for the response in advance!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this:
Code:
Private Sub UserForm_QueryClose (Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        MsgBox "Don even think about it"
        Cancel = True
    End If
End Sub
 
Upvote 0
Adapted from http://www.oaltd.co.uk/Excel/Default.htm 'NoCloseButton.zip'.

In the UserForm's code module

Code:
Private Sub UserForm_Initialize()
HideCloseButton Me
End Sub

Private Sub CommandButton1_Click()
Unload Me
End Sub

In a regular module

Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const WS_SYSMENU = &H80000
Const GWL_STYLE = (-16)

Sub HideCloseButton(oDialog As Object)
Dim hWnd As Long, lStyle As Long
hWnd = FindWindow("ThunderDFrame", oDialog.Caption)
lStyle = GetWindowLong(hWnd, GWL_STYLE)
SetWindowLong hWnd, GWL_STYLE, lStyle And Not WS_SYSMENU
End Sub

Sub ShowUserForm()
frmNoClose.Show vbModeless
Application.Wait Now() + TimeValue("00:00:03")
frmNoClose.Hide
End Sub

ShowUserForm is just an example to show the UserForm for 3 seconds.
 
Upvote 0
thanks for the quick response guys.. appreciate the help..exactly what i was lookin for.
 
Upvote 0

Forum statistics

Threads
1,216,066
Messages
6,128,571
Members
449,458
Latest member
gillmit

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top