Password in order to save workbook...SOLVED

dsnbld

Board Regular
Joined
May 7, 2002
Messages
206
For the record I did a search on passwords, read them all. Also have had my nose in Excel help file. Trying, but stuck. I know about Sub Form_Load(), but not sure how to implement it.
I'll bet I've gone about this wrong, but here's what I've got-

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
UserForm1.Show
End Sub

Private Sub CommandButton1_Click()
If TextBox1 = "pw" Then
MsgBox "Password Accepted", vbInformation
Unload Me
Else
If TextBox1<> "pw" Then
MsgBox "Invalid Password", vbOKOnly + vbCritical
Unload Me
***What Can I put here to abort the save?***
End If
End If
End Sub
This message was edited by dsnbld on 2002-05-11 03:09
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
You could try cancelling the save before you show the userform. Then if the user gets the password right save the workbook yourself.

Either that or just hide the useform when the user clicks on OK and change your code to

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

UserForm1.Show

If UserForm1.TextBox1 <> "pw" Then
Cancel = True
End If

Unload UserForm1

End Sub
 
Upvote 0
Good work Gary- Thanks. I thought I tried cancel... Seems to be working fine, just got to disable the close command on the form.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
UserForm1.Show
If UserForm1.TextBox1 <> "pw" Then
Cancel = True
End If
Unload UserForm1
End Sub

Private Sub CommandButton1_Click()
If TextBox1 = "pw" Then
MsgBox "Password Accepted", vbInformation
UserForm1.Hide
Else
If TextBox1 <> "pw" Then
MsgBox "Invalid Password", vbOKOnly + vbCritical
Unload UserForm1
End If
End If
End Sub
 
Upvote 0
I use this code. It will only allow a user to save the file if the correct password is given and will have a pop-up that says "not saved...." if it is incorrect:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim Password As String
Dim EnteredPassword As String

Password = "Secret"
EnteredPassword = InputBox("Enter password to save changes")
If EnteredPassword <> Password Then
Cancel = True
MsgBox ("Password incorrect, file not saved")
End If

End Sub


Mav
 
Upvote 0
Mav, that is too fine. The Sub Form_Load() I was talking about is basically what you've got, but I had trouble getting it to work. Kept missing some little something.
Great work guys. I should have posted a long time ago, but I learned quite a bit in the process.
 
Upvote 0

Forum statistics

Threads
1,213,550
Messages
6,114,265
Members
448,558
Latest member
aivin

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