Power Query Course in Spanish
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 5 of 5

Thread: Password in order to save workbook...SOLVED

  1. #1
    Board Regular
    Join Date
    May 2002
    Posts
    206
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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 ]

  2. #2
    New Member
    Join Date
    Feb 2002
    Location
    Birmingham
    Posts
    13
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  3. #3
    Board Regular
    Join Date
    May 2002
    Posts
    206
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  4. #4
    New Member
    Join Date
    Mar 2002
    Posts
    18
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  5. #5
    Board Regular
    Join Date
    May 2002
    Posts
    206
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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.

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •