Doug Logan
Doug Logan

Reputation: 39

Excel VBA UserForm - Minimise Button

Hi All,

I have been working on a couple user forms within Excel VBA for a few days now along with assistance from another gentleman on an issue. Whilst working on them I realised it's a nightmare as I can't click off of them onto my spreadsheet, which is what has lead me to ask the question; Is there a way to create a minimize button next to the close icon at the top right of the userform? I would prefer this to just a random button, but if either is possible, would anyone be able to assist. I have already attempted it .hide, but that doesn't appear to work.

If it helps, I am using Excel 2010

Thanks in advance :)

Upvotes: 0

Views: 3316

Answers (1)

saransh
saransh

Reputation: 200

Try like this:-

Private Sub UserForm_Initialize()
InitMaxMin Me.Caption
End Sub

Put This code in module

Option Explicit
Public Declare Function FindWindowA& Lib "user32" (ByVal lpClassName$, ByVal lpWindowName$)
Public Declare Function GetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&)
Public Declare Function SetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&, ByVal dwNewLong&)
' Déclaration des constantes
Public Const GWL_STYLE As Long = -16
Public Const WS_MINIMIZEBOX = &H20000
Public Const WS_MAXIMIZEBOX = &H10000
Public Const WS_FULLSIZING = &H70000
'Attention, envoyer après changement du caption de l'UF
Public Sub InitMaxMin(mCaption As String, Optional Max As Boolean = True, Optional Min As Boolean = True _
        , Optional Sizing As Boolean = True)
Dim hwnd As Long
    hwnd = FindWindowA(vbNullString, mCaption)
    If Min Then SetWindowLongA hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) Or WS_MINIMIZEBOX
    If Max Then SetWindowLongA hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) Or WS_MAXIMIZEBOX
    If Sizing Then SetWindowLongA hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) Or WS_FULLSIZING
End Sub

Upvotes: 1

Related Questions