Use same button that opens userform to close userform

SantanaKRE8s

Board Regular
Joined
Jul 11, 2023
Messages
130
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

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Please try this:

Code that needs to be put in a Standard module. UFLoaded is used to test if the form is loaded
VBA Code:
Public UFLoaded As Boolean

Sub ToggleUserForm()
  
  If UFLoaded = True Then
    Unload UserForm1
    UFLoaded = False
  Else
    UserForm1.Show
  End If
  
End Sub

Code for the button. This needs to be placed on the sheet module where the button resides
VBA Code:
Private Sub CommandButton1_Click()
  ToggleUserForm
End Sub

Two Subs that need to be put in the Form level. The first SUB tests if the form is closed using the X at the top right of the form. If you use any buttons on the form to close it, you'll need to add 'UFLoaded = False' to that button code. You may already have the UserForm_Initialize SUB, so just add that line 'UFLoaded = True'.
VBA Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then
      UFLoaded = False
      
        ' Your codes
        ' Tip: If you want to prevent closing UserForm by Close (×) button in the right-top corner of the UserForm, just uncomment the following line:
        ' Cancel = True
    End If
End Sub


Private Sub UserForm_Initialize()
  
  UFLoaded = True
  
  'ListBox1.List = Sheets("Sheet1").Range("D2:G31").Value
  
End Sub
 
Upvote 0
I will only be using the button I created and that I show on the image I shared, so the third code I dont need correct?
 
Upvote 0
You could get rid of the third code if you alter this code

Sub ToggleUserForm()

If UFLoaded = True Then
Unload UserForm1
UFLoaded = False
Else
UFLoaded = True
UserForm1.Show
End If

End Sub
 
Upvote 0
Remvoed all the codes and just added the two first ones, the userform opens but not closing using the same object to ope it
 
Upvote 0
Not quite enough info for me to be more explicit, but my approach mibht be like the following.

In the beginning, your sheet button (or is that a userform?) caption seems to be QUIKview. So its click event would be something like

VBA Code:
If myButton.Caption = "QUIKview" Then
   'open some form
   myButton.Caption = "Close QUIKview"
Else
   Unload SomeForm
End If
 
Upvote 0
The @Micron approach is the approach I was going to suggest. Each time the button is clicked, change the caption of the button, run the code to either open or close the form.
 
Upvote 0
Try
Code:
Sub AAAAA()
    If UserForm1.Visible = True Then Unload UserForm1 Else UserForm1.Show vbModeless
End Sub
 
Upvote 0
Solution
So I removed everything again, Micron because your option seems simple. Below images LEFT to RIGHT First image is my object (button) that I am using to open my userform called "QUIKview" which is the second image. Ant the thrid image is of my modules and Userform. I want the same button to open and close the userform.





1691447801192.png
1691447828525.png
1691447860703.png
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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