+ Reply to Thread
Results 1 to 2 of 2

password protect workbook

  1. #1
    rob
    Guest

    password protect workbook

    Very new to VB. Have managed to paste this code into a workbook:

    Option Explicit

    Private Sub Workbook_Open()
    Worksheets("Sheet1").Activate
    End Sub

    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Dim ans
    Dim sh2 As Worksheet
    Application.EnableEvents = False
    If ActiveSheet.Name <> "Sheet1" Then
    Set sh2 = ActiveSheet
    Sh.Activate
    Application.ScreenUpdating = False
    ans = Application.InputBox("Supply password")
    If ans <> False Then
    If ans = "password" Then
    sh2.Activate
    End If
    End If
    End If
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    End Sub

    It works great, but would like to modify it so that I only need to enter the
    password once to gain access to all worksheets, not each time I change
    worksheets.

    Much appreciated,
    rob


  2. #2
    Jim Rech
    Guest

    Re: password protect workbook

    Create a module level variable like Dim PasswordOK As Boolean. Set this to
    True when the password is correctly entered. Then modify your code to check
    this variable and run only is it is False.

    --
    Jim
    "rob" <rob@discussions.microsoft.com> wrote in message
    news:4C665376-53CE-438F-BAE1-C429CDE92A5E@microsoft.com...
    | Very new to VB. Have managed to paste this code into a workbook:
    |
    | Option Explicit
    |
    | Private Sub Workbook_Open()
    | Worksheets("Sheet1").Activate
    | End Sub
    |
    | Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    | Dim ans
    | Dim sh2 As Worksheet
    | Application.EnableEvents = False
    | If ActiveSheet.Name <> "Sheet1" Then
    | Set sh2 = ActiveSheet
    | Sh.Activate
    | Application.ScreenUpdating = False
    | ans = Application.InputBox("Supply password")
    | If ans <> False Then
    | If ans = "password" Then
    | sh2.Activate
    | End If
    | End If
    | End If
    | Application.ScreenUpdating = True
    | Application.EnableEvents = True
    | End Sub
    |
    | It works great, but would like to modify it so that I only need to enter
    the
    | password once to gain access to all worksheets, not each time I change
    | worksheets.
    |
    | Much appreciated,
    | rob
    |



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1