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
Update: OP seems to be having trouble finding this thread so I sent a link. It seems the object ("button caption") text change was not desired. Since I have the file it's easier for me to see where things are - it's not that I'm trying to control responses or anything and I'm trying to be open about the process.

I added 5 lines of code and some comments to explain it. Seems to do what is desired.
VBA Code:
Sub UserForm()
Dim i As Integer

'Typical approach for zero based collection is to use For i = 0 To [collection].Count-1
'If there is only 1 form open that counter will become For i = 0 to 0 (because of 1-1)
'This approach makes the counter from 1 to 1 when only 1 form is open
'and Userforms(i-1) produces the correct zero based starting point for the collection

'If count = 0 then no form is open, skip to With block, else test if open form name is QUIKview.
'If true, close it and exit sub
For i = 1 To VBA.UserForms.Count
     If VBA.UserForms(i - 1).Name = "QUIKview" Then Unload VBA.UserForms(i - 1)
     Exit Sub
Next

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
Micron will you be making the change and adding the code in the file I sent you and send it back to me?
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Micron will you be making the change and adding the code in the file I sent you and send it back to me?
I tried adding this code in my module but it opens the userform but not closing it, which is why I was asking if you could do it in the file I sent you and send it back to see what Im doing wrong. I also agree and would like if we could chamge it like you mentioned so it will not be Modal, and I can manipiulate the sheet while user form is is open.
 
Upvote 0
I have been watching this posting and not sure I have seen an answer or am I missing something.
I would think it should be a simple thing to toggle the userform from shown to not shown but I guess not.
Seeing that this is post number 33.
 
Upvote 0
I have been watching this posting and not sure I have seen an answer or am I missing something.
I would think it should be a simple thing to toggle the userform from shown to not shown but I guess not.
Im a bit new to VBA and I believe from the begining I was not providing enough information or was not clear.
 
Upvote 0
You're trying with one button to toggle the userform from shown to not shown is that true?
 
Upvote 0
Your trying with one button to toggle the userform from shown to not showed is that true?
Yes that is correct, I have so many buttons I want to make this one to open and close my user form. also this is not an actual button, its an object I created and named QUIKview.

1691523053800.png
 
Upvote 0
I have used UserForms for years and I normally put button on the userform and use the code Unload me to close the user form.
Not sure why you want to put a button on your worksheet to close the user form.
 
Upvote 0
I have used UserForms for years and I normally put button on the userform and use the code Unload me to close the user form.
Not sure why you want to put a button on your worksheet to close the user form.
This userform just has information that updates and show the date of last change made to worksheet. Just a simple way to see results at a glance.
 
Upvote 0
You may not know but instead of using a lot of buttons you can use a script where if you double click a cell a script will run.
If your interested let me know.
 
Upvote 0
You may not know but instead of using a lot of buttons you can use a script where if you double click a cell a script will run.
If your interested let me know.
I would not mind trying that on a diferent DOC, but for this one I would like to make this happen. can you help.
 
Upvote 0

Forum statistics

Threads
1,216,445
Messages
6,130,685
Members
449,585
Latest member
Nattarinee

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