unlock / hide sheets with password

Cablek

Board Regular
Joined
Nov 22, 2017
Messages
51
I currently have a workbook that when Costing Sheet or Costing Work Sheet sheets are clicked on it requires a password... the problem is that once the user enters this password then clicks on the next sheet he then needs to enter the password again when he goes back...

Is there anyway to have it so once the password have been entered it unlocks the 2 sheets for the duration that the file is open?

current code for each sheet is

Private Sub Worksheet_Activate()
Dim strPassword As String
On Error Resume Next
Me.Protect Password:="Purchaser"
Me.Columns.Hidden = True
strPassword = InputBox("Enter password to access Costing sheet")
If strPassword = "" Then
ActiveSheet.Visible = False
Worksheets("Menu").Select
Exit Sub
ElseIf strPassword <> "Purchaser" Then
MsgBox "Password Incorrect "
ActiveSheet.Visible = False
Worksheets("Costing sheet").Select
Exit Sub
Else
Me.Unprotect Password:="Purchaser"
Me.Columns.Hidden = False
End If
Range("a1").Select
On Error GoTo 0
End Sub
Private Sub Worksheet_Deactivate()
On Error Resume Next
Me.Columns.Hidden = True
On Error GoTo 0
End Sub

same for the 2nd sheet but different name
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Perhaps the use of a public Boolean variable set to TRUE the first time the sheet is activated
and its state being checked at the beginning of sub.
Code:
Option Explicit
Dim alreadyused As Boolean

Private Sub Worksheet_Activate()
Dim strPassword As String

If alreadyused Then
    Me.Unprotect Password:="Purchaser"
    Me.Columns.Hidden = False
    Exit Sub
End If

On Error Resume Next
Me.Protect Password:="Purchaser"
Me.Columns.Hidden = True

strPassword = InputBox("Enter password to access Costing sheet")

If strPassword = "" Then
    ActiveSheet.Visible = False
    Worksheets("Menu").Select
    Exit Sub
ElseIf strPassword <> "Purchaser" Then
    MsgBox "Password Incorrect "
    ActiveSheet.Visible = False
    Worksheets("Costing sheet").Select
    Exit Sub
Else
    Me.Unprotect Password:="Purchaser"
    Me.Columns.Hidden = False
    alreadyused = True
End If

Range("a1").Select
On Error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,578
Members
449,174
Latest member
chandan4057

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