Use same button that opens userform to close userform

SantanaKRE8s

Board Regular
Joined
Jul 11, 2023
Messages
121
Office Version
  1. 365
Platform
  1. Windows
I have a button that I created from an object and I use it to open my userform, is there a VBA code to use the same button that opens the userform to also close the userform, so that I dont have to add another button. I already have a lot of buttons want to avoid more. attached is an image
 

Attachments

  • Button to open and close.png
    Button to open and close.png
    80.5 KB · Views: 19
@ My Answer Is This
"QuickView" (not Quit view), without double quotes, is the name of the UserForm that needs to be opened and closed. Actually it is called "QUICKview" I think. Check before getting too excited about the whole thing.
So you need to have a UserForm by that name in your Workbook.
If you have the first code from Post #47 in a regular module and your changed code from that same post in the sheet module and you double click in Cell A1 it will open, if it is closed, or close, if it is open, the userform.
It does on my computer anyway.
@ Santana
You must not have tried the supplied code.
If you tried the code in Post #47 you would see that it does, open and close to take a quick view.
@ Micron
Re: Because when the form is modal you can't click on the shape on the sheet to close the same form.
That's why I have it as modeless!!!!!!
Re: It was decided that it does not need changing.
In which post was that decided? We are not privy to the PMs
See Post #9 and Post #26, both without changing text, and Post #47 (with changing text).
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
@ My Answer Is This
"QuickView" (not Quit view), without double quotes, is the name of the UserForm that needs to be opened and closed. Actually it is called "QUICKview" I think. Check before getting too excited about the whole thing.
So you need to have a UserForm by that name in your Workbook.
If you have the first code from Post #47 in a regular module and your changed code from that same post in the sheet module and you double click in Cell A1 it will open, if it is closed, or close, if it is open, the userform.
It does on my computer anyway.
@ Santana
You must not have tried the supplied code.
If you tried the code in Post #47 you would see that it does, open and close to take a quick view.
@ Micron
Re: Because when the form is modal you can't click on the shape on the sheet to close the same form.
That's why I have it as modeless!!!!!!
Re: It was decided that it does not need changing.
In which post was that decided? We are not privy to the PMs
See Post #9 and Post #26, both without changing text, and Post #47 (with changing text).
 
Upvote 0
OK I see now.

Here is how I would do it.
I like select case.

VBA Code:
Sub RectangleRoundedCorners1_Click()

Select Case UserForm1.Visible
    Case True: Unload UserForm1
    Case False: UserForm1.Show modeless
End Select
End Sub

Or whatever the name of your userform is:
 
Upvote 0
In which post was that decided? We are not privy to the PMs
I stated that in post 50. As for code in post 53, it opens the form but doesn't close it. I've read that referring to a form in some ways causes it to initialize again (if that is the correct term for what happens). I put a message box in form initialize and QueryClose events. I only get the opening message once when the form opens and the close message only once when I close it with the form x button. So it seems that it just isn't closing even though the line is executed. That or a form in memory simply "re-displays" when you refer to it.

I was waiting for the OP to post back about whether or not what I wrote worked. It works for me as long as there is only one form open, which is how the file works now. However, I forced another form to be open at the same time and that produces an issue.

Since I have the file, I will test other posted suggestions that were so graciously provided and see what happens.
EDIT - I'm beginning to think that a module level variable will be required as was suggested long ago.
 
Upvote 0
Adapted from post 4 by @Jeffery Mahoney, this works whether or not the other form is open.
VBA Code:
Public UFLoaded As Boolean

Sub UserForm()
  
If UFLoaded = True Then
    Unload QUIKview
    UFLoaded = False
Else
    UFLoaded = True
    QUIKview.Show
End If

With QUIKview
     .StartUpPosition = 0
     .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
     .Top = Application.Top + (0.5 * Application.Height) - (0.3 * .Height)
     '.Show
End With

End Sub
 
Upvote 0
Adapted from post 9 by @jolivanes, this also works
VBA Code:
If QUIKview.Visible = True Then
     Unload QUIKview
Else
     QUIKview.Show vbModeless
End If

With QUIKview
     .StartUpPosition = 0
     .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
     .Top = Application.Top + (0.5 * Application.Height) - (0.3 * .Height)
     '.Show
End With
 
Upvote 0
To illustrate what I wrote before:

Form is not open, click the shape...
1691595028660.png


This line ...

1691595068676.png


makes the form load, so it is always true, thus it always closes.
1691595097865.png
 
Upvote 0
REceived file shared with Miron and its the solution. Cannot share code since its very long but if needed I will share it.
 
Upvote 0
since its very long
Then you must have modified it. I only added 5 lines regardless if you use what I sent you or what you've marked as the solution. Maybe you should mark post 9 as the solution.
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,005
Members
449,203
Latest member
Daymo66

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