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

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
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,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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