如何从excel userform中删除close(x)选项?

use*_*978 2 excel vba excel-vba

如何从excel userform中删除close(X)选项?我得到了已经讨论过的以下链接. http://www.excelforum.com/excel-programming-vba-macros/694008-remove-user-form-b​​orders.html

但是我得到他们给的东西,请帮帮我....

Rea*_*idy 6

首先:确保至少包含一种明显的方法来关闭表格!!

在点击关闭之后,我不会使用消息框打扰最终用户,而是完全隐藏关闭:

'//Find the userform's Window
Private Declare Function FindWindow Lib "user32" _
        Alias "FindWindowA" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long

'//Get the current window style
Private Declare Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongA" ( _
        ByVal hWnd As Long, _
        ByVal nIndex As Long) As Long

'//Set the 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

Private Sub UserForm_Initialize()
   Dim hWnd As Long, lStyle As Long

   If Val(Application.Version) >= 9 Then
      hWnd = FindWindow("ThunderDFrame", Me.Caption)
   Else
      hWnd = FindWindow("ThunderXFrame", Me.Caption)
   End If

   '//Get the current window style and turn off the Close button
   lStyle = GetWindowLong(hWnd, GWL_STYLE)
   SetWindowLong hWnd, GWL_STYLE, (lStyle And Not WS_SYSMENU)
End Sub
Run Code Online (Sandbox Code Playgroud)

如果你想通过ALT-F4阻止关闭,那么也要使用它:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
      If CloseMode = vbFormControlMenu Then
        Cancel = True  
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

  • 您可能希望添加即使隐藏关闭按钮,用户仍然可以使用Alt + F4关闭窗口 (2认同)