activate worksheet with single password entry

dodson

New Member
Joined
Apr 5, 2011
Messages
5
Hi Everyone...I'm a newbie here.

Desired Scenario,

1. User opens spreadsheet.
2. Spreadsheet has two worksheets
3. User selects to view "Sheet2".
4. User is prompted for password (once for the entire file session).
5. Correct password (softball) allows access to worksheet, incorrect password returns user to Sheet1.

I can create this scenario with password entry required each time Sheet2 is selected, however I would like to avoid multiple password entries if possible.

I have attempted the code offered here - http://www.mrexcel.com/archive/VBA/1646.html - with no luck.

I currently have functional code to ensure macros are enabled to view the worksheets in question...I just need help sorting out this multiple password entry if possible.

Thanks in advance for any help offered!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
In ThisWorkbook module:
Code:
Private Sub Workbook_Open()
With Sheets("Sheet2")
    .Range("A1").Value = 0
End With
End Sub
In Sheet2 module:
Code:
Private Sub Worksheet_Activate()
Dim rng As Range, ans As String
Set rng = Range("A1")
If rng.Value > 0 Then Exit Sub
ans = InputBox("Enter the password")
If ans <> "softball" Then
    Sheets("Sheet1").Select
Else
    rng.Value = rng.Value + 1
End If
End Sub
Cell A1 in sheet2 is used to store the number of times this sheet is activated while the workbook is opened. You can use a different cell by altering the cell address in both modules.
 
Upvote 0
Thanks Joe,
That code worked flawlessly.

I have one issue with it. When Sheet2 is selected, the worksheet is visible in the background behind the password input box. For non-pwd users, I wouldn't want the information on the worksheet to be visible at all. Any suggestions how to prevent this?
 
Upvote 0
Thanks Joe,
That code worked flawlessly.

I have one issue with it. When Sheet2 is selected, the worksheet is visible in the background behind the password input box. For non-pwd users, I wouldn't want the information on the worksheet to be visible at all. Any suggestions how to prevent this?
Amend the Sheet2 code to this:
Code:
Private Sub Worksheet_Activate()
Dim rng As Range, ans As String

Set rng = Me.Range("A1")
If rng.Value > 0 Then
    Me.Visible = xlSheetVisible
    Exit Sub
Else
    Me.Visible = xlSheetHidden
End If
ans = InputBox("Enter the password")
If ans = "" Or ans <> "softball" Then
    Sheets("Sheet1").Activate
    Me.Visible = xlSheetVisible
    Exit Sub
Else
    rng.Value = rng.Value + 1
    Me.Visible = xlSheetVisible
    Me.Activate
End If
End Sub
 
Upvote 0
once again...worked flawlessly! thanks a ton!

I was also able to incorporate an "incorrect password" message myself...and force the workbook to open to Sheet1.

Slowly but surely I'm learning.

Thanks again for your time and code help!!
 
Upvote 0
new problem...I have the workbook functioning as desired...now I want to protect the worksheets and the workbook to prevent users from inadvertently altering formulas.

I protected the workbook and now when I open the workbook and select Sheet2, I get the following error

Run-time error '1004':
Method 'Visible' of object '_Worksheet' failed

The 'debug' button points me to line 6 of the following code (located in the Sheet2 module):
Code:
Set rng = Me.Range("A4")
If rng.Value > 0 Then
   Me.Visible = xlSheetVisible
    Exit Sub
Else
    Me.Visible = xlSheetHidden
End If
ans = InputBox("Enter the password")
If ans = "" Or ans <> "softball" Then
    MsgBox "Incorrect Password", vbCritical, "You are not allowed to view the selected worksheet"
    Sheets("Sheet1").Activate
    Me.Visible = xlSheetVisible
    Exit Sub
Else
    rng.Value = rng.Value + 1
    Me.Visible = xlSheetVisible
    Me.Activate
End If
End Sub

Apparently the workbook protection is interfering with the worksheet code. Any ideas?
 
Upvote 0
I was able to resolve the workbook protection problem...I used "protect worksheet" instead and all seems to work well.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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