With CommandButton would like to automatically minimize the Userform.

NimishK

Well-known Member
Joined
Sep 4, 2015
Messages
684
Hi

This is my first time working on API for Minimizing, Maximizing Buttons on Userform and Found the below following code at https://stackoverflow.com/questions/50853247/excel-vba-userform-minimise-button

Would it be possible for a command button Click to automatically minimize the userform and show userform modeless
'Code in Module
Code:
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

Code in Userform
Code:
Private Sub UserForm_Initialize()
    InitMaxMin me.Caption
End Sub

Private Sub CommandButton1_Click()
    userform1.show vbModeless
End Sub

what needs to be coded when clicked on command button: Automatically the Userform minimises automatically rather then clicking on
minimize button on userform and show userform modeless

thanks
NimishK
 
Last edited:
Re: With Commnand_Button would like to automatically minimize the Userform.

Guess you have now found what you want.​
 
Last edited:
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Re: With Commnand_Button would like to automatically minimize the Userform.

Yongle you are really working hard.
Public FormHidden As Boolean
In userform module
Code:
Private Sub CommandButton1_Click() 'button to allow you to work in worksheet
'amend boolean variable
FormHidden = True
'store values in hidden sheet
Sheets("HiddenSheet").Range("A1") = TextBox1.Value
Unload Me
End Sub

Private Sub UserForm_Initialize()
If FormHidden = True Then
'get values from hidden sheet...
TextBox1.Value = Sheets("HiddenSheet").Range("A1").Value
'amend boolean variable
FormHidden = False
'delete values in hidden sheet
Sheets("HiddenSheet").Range("A1").ClearContents
Else
'default userform values
End If
End Sub

My dear there is a major difference in Minimizing a form and Hiding a Form basically when form will be mninimized the values in Textfields are already there on the form intact(Hopefully As this is VBA excel userform). The Userform is Minimized and you can see the same in front of you but in smaller size.

Tgis solution may not even be practical if the userform is very complicated or is affected by other userforms etc
Without MyAnswerIsThis's solution whether its practical or impractical will be determined after he posts his script.
 
Last edited:
Upvote 0
Re: With Commnand_Button would like to automatically minimize the Userform.

I asked you earlier two questions and you never gave me a answer.

What size do you want to minimize the Userform to?

If I have a house 100 square foot in size and I say I want my house minimized
I must say I now want my house 40 square foot.

I could write a script that makes it half the size it now is but then you may say O no that's not what I want.

But then I would say well that's because you have not given me specific details.
If you want specific results you must provide specific details.
 
Last edited:
Upvote 0
Re: With Commnand_Button would like to automatically minimize the Userform.

My Answer is this:

Actually i did not follow you question as per your post #2

To be specific size should be sligtly bigger then Minimize Maximize and Close button(s) and UF width showing at least UF caption with 3 Buttons minimize maximize and Close Button.

thanks
 
Upvote 0
Re: With Commnand_Button would like to automatically minimize the Userform.

Well you are going to need do this yourself.

You would need to do some testing to see how this works.

In one of your buttons you would need a script something like the:

Button1.Width=20
Button1.Height=20
Button2.Width=20
Button2.Height=20
Useform1.Width=100
Userform1.Height=100

And you would have to do this with all the buttons on your Userform

Button1 being the name of the Button

And then keep adjusting things to what you want.
 
Upvote 0
Re: With Commnand_Button would like to automatically minimize the Userform.

Or try a script like this.
It will change the size of all the controls in your Userform
And your Userform.
Me means active userform
Practice with it and see how it works.

Code:
Private Sub CommandButton4_Click()
'Modified  10/14/2018  1:52:55 PM  EDT
    For Each Control In Me.Controls
        Control.Width = Control.Width - 1
        Control.Height = Control.Height - 1
    Next
Me.Height = Me.Height - 1
Me.Width = Me.Width - 1
End Sub
 
Last edited:
Upvote 0
Re: With Commnand_Button would like to automatically minimize the Userform.

My Ans is this. Really thankx for your suggestion.
Yongle and you have missed the Part "When Clicked on command button (fires) : The Userform has to be Mimimized like when clicked on Minimised button of Userform. in other words CommandButton_click = Minimized button of UForm clicked

Also i came up with Idea after both of yours suggestion

Kindly try at your end

I changed property of
Userform1.ShowModal = False
then code in module
Code:
Option Explicit
'https://stackoverflow.com/questions/50853247/excel-vba-userform-minimise-button
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

Sub Button1_Click()
'yongle's : Button1 on sheet1 
Load UserForm1
UserForm1.Show vbModeless
End Sub
Code in UForm
Code:
Private Sub UserForm_Initialize()
   InitMaxMin Me.Caption
End Sub
Basically everything remains in tact in textbox and nothing looses and also able to work on worksheet but only When clicked on minimized button of UF but not achieved as per desired Target. :(
 
Last edited:
Upvote 0
Re: With Commnand_Button would like to automatically minimize the Userform.

Well I guess neither of us know what your wanting. I wish you the best and hope someone else can provide you a answer. I will continue to monitor this thread to see what I can learn
 
Upvote 0
Re: With Commnand_Button would like to automatically minimize the Userform.

You keep posting code you found on Stackoverflow. That I do not understand and I do not plan to test. So if you have tested this script and it does what you want. Then I do not understand what your needs are.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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