Excel

Add a Minimize Button to a Userform

Ease of Use

Intermediate

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

This macro will add a standard minimize button to the UserForm. 

Discussion:

You have a UserForm and you want to allow the user to be able to minimize like they can with most applications. This macro will add the minimize button. 

Code:

instructions for use

			

'Place this code in a Module 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 Option Explicit Sub FormatUserForm(UserFormCaption As String) Dim hWnd As Long Dim exLong As Long hWnd = FindWindowA(vbNullString, UserFormCaption) exLong = GetWindowLongA(hWnd, -16) If (exLong And &H20000) = 0 Then SetWindowLongA hWnd, -16, exLong Or &H20000 Else End If End Sub Sub ShowForm() UserForm1.Show End Sub 'Place this code in a UserForm with one Command Button named CommandButton1. Option Explicit Private Sub CommandButton1_Click() Unload Me End Sub Private Sub UserForm_Initialize() Call FormatUserForm(Me.Caption) End Sub

How to use:

  1. Open Excel.
  2. Alt + F11 to open the VBE.
  3. Insert | Module.
  4. Paste the code from above that is designated for the Module.
  5. Insert | UserForm.
  6. Double click the userform and paste the code from above designated for the UserForm.
  7. Add one command button to the UserForm from the Control Toolbox.
  8. Close the VBE (Alt + Q or press the X in the top right corner).
 

Test the code:

  1. Tools | Macro | Macros...
  2. Select ShowForm and press Run.
 

Sample File:

Add Minimize Button To Userform.zip 7.87KB 

Approved by mdmackillop


This entry has been viewed 326 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express