Can you take the button to close forms in VBA/Excel?

Asked

Viewed 4,840 times

2

I want to take the close button of some forms in VBA/ Excel.

For example, I am customizing the presentation of some messages to differentiate from VBA options that are unattractive.

In addition to using different images, colors and fonts, it would be interesting if the form that will display the message did not display the close button (the "X" in the upper right corner), forcing the user to choose only "Yes" or "No", for example, and add the "Cancel" button where this option exists as well.

You can do it?

I found some solutions, but they are for old versions of VBA and for 32 bits, I could not adapt them to work in 64 bits.

  • I removed the sign sorry for the misunderstanding. @leo

  • Thank you! Thank you.

  • 1

    It seems you have two questions of your own with the same title. @leo

  • 1

    I realized, I just removed, grateful once again.

1 answer

3


The solution I found is on the Tomas Vasquez Sites.

DISABLING THE CLOSE BUTTON OF A USERFORM IN VBA

This is the code:

Option Explicit

' Fonte: Tomas Vasquez Sites
'
' http://www.tomasvasquez.com.br/blog/microsoft-office/vba/desabilitando-o-botao-fechar-de-um-userform-no-vba

'==========================================================================
' Retira o botão "X" (fechar) do Formulário, permanecem a barra e o caption
'==========================================================================

Private Declare Function FindWindowA Lib "user32" _
         (ByVal lpClassName As String, _
          ByVal lpWindowName As String) _
             As Long

Private Declare Function GetWindowLongA Lib "user32" _
         (ByVal hwnd As Long, _
          ByVal nIndex As Long) _
             As Long

Private Declare Function SetWindowLongA Lib "user32" _
         (ByVal hwnd As Long, _
          ByVal nIndex As Long, _
          ByVal dwNewLong As Long) _
             As Long

Private Sub UserForm_Initialize()

Dim hwnd As Long

hwnd = FindWindowA(vbNullString, Me.Caption)

SetWindowLongA _
 hwnd, -16, _
 GetWindowLongA(hwnd, -16) And &HFFF7FFFF

End Sub


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

Dim hwnd As Long

hwnd = FindWindowA(vbNullString, Me.Caption)

SetWindowLongA _
 hwnd, -16, _
 GetWindowLongA(hwnd, -16) Or &H80000

End Sub


Private Sub ButtonSair_Click()

Unload Me

End Sub

The form needs a close button in this case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.