Password to close workbook

bhsoundman
05-21-2010, 04:14 PM
I've got a worksheet intended for public use & want to prevent them from closing it. I've gotten to a point where once they initiate closing that it pops up with a user form requesting a password. If they try to close out of that via the X, it tells them a password is required. When I enter the password, it lets the normal Windows close dialog appear & I can chose to save or cancel etc. So all of that works fine. The problem that I am experiencing is when the user attempts to cancel out of the password form. I would like for it to cancel the worksheet close request that is in progress, but it currently only let the windows dialog pop up & then the user can close from there.

Bottom line is I would like to abort a windows close command that is already in progress and go back to the worksheet.

Any ideas?

Thanks in advance!!

Josh Hazel
05-21-2010, 09:49 PM
You should just use a simple inputbox instead, no need for a userform.

In ThisWorkbook in VBA, add the following code

Private Sub Workbook_BeforeClose(Cancel as Boolean)
'Declare your string
dim strPassword as string
'Capture the password entered in the inputbox
strPassword = inputbox("Please provide the password to close workbook:")
'Test the password, if no match show msgbox saying invalid, then cancel the close
if not strPassword = "Password" then msgbox "Invalid password.": cancel = true
End Sub


Only benefit of a userform is that you can mask the text. Ive found a class someone has created in the past though that allows the use of a masked inputbox and can track that down if you need to, or you can use the userform you sugg.

bhsoundman
05-21-2010, 10:39 PM
Thank You!!!! Works perfectly!:D

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum