How to close userform if another is open

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,227
Office Version
  1. 2010
Platform
  1. Windows
Hi please could you help me
I have 2 userform called userform 1 and userform 2. If someone has userform 1 already open and someone else trys to open userform 2 I want it to automatically close userform 2 with a message to try later. Can you help me please?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Put this in the Intialize event for userform2

Code:
Private Sub UserForm_Initialize()
    If 1 < UserForms.Count Then 
        MsgBox "try later"
        Unload Me
    End If
End Sub
 
Upvote 0
hi thank you for the code :) I for got to mention that userform 1 ius located in sheet1 and userform 2 is located in sheet2, how do I amend the code please to reflect that? thank you for your help this is greatly appreciated :) I am still learning :)
 
Upvote 0
Hi sorry me again, just wondering if you had any ideas to add the sheet1 and sheet2 along side the userforms at all please, I am very grateful for your time
 
Upvote 0
hi s is what I have amended it to, (please see below) to try and add in the sheet1 but its not working, hope you nan help?
Code:
PrivateSub UserForm_Initialize()
 
DimWB As Workbook: Set WB = ThisWorkbook
DimWS As Worksheet
SetWS = WB.Sheets("Sheet1")

 
   If ws = 1 < userforms.Count Then
       MsgBox "try later"
       Unload Me

   End If
EndSub
 
Last edited by a moderator:
Upvote 0
Userforms are not located in sheets. There is no need to reference the worksheet.

What happened when you tried my suggestion.
 
Last edited:
Upvote 0
Hi thanks for your reply. To get to the userform I click on command button 3 do I put the code under there?
 
Upvote 0
Ok I try that. Do I right click on userform2 and put the code in that way?
 
Upvote 0
Hello just wondered as I have in total 5 userforms but only want the code to work for userform 1 and 2 is this still ok? Thank you again for your time
 
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,203
Members
448,951
Latest member
jennlynn

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