ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   password protect workbook (https://www.excelbanter.com/excel-discussion-misc-queries/105130-password-protect-workbook.html)

Rob

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


Jim Rech

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" wrote in message
...
| 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
|




All times are GMT +1. The time now is 08:02 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com