Using VB to password protect, but sheet remains visible

J

JennyJeneralGraves

I used the following code to cause a password prompt to display upon clicking
on a tab in my workbook:

Private Sub Worksheet_Activate()
Dim strPassword As String
On Error Resume Next
Me.Protect Password:="123"
Me.Columns.Hidden = True

strPassword = InputBox("Enter password to view this sheet:")

If strPassword = "" Then
Me.Previous.Select
Exit Sub
ElseIf strPassword <> "datagility" Then
MsgBox "Password Incorrect", , "Wrong password"
Me.Previous.Select
Exit Sub
Else
Me.Unprotect Password:="123"
Me.Columns.Hidden = True
End If
On Error GoTo 0
End Sub

Private Sub Worksheet_Deactivate()
On Error Resume Next
Me.Columns.Hidden = True
On Error GoTo 0
End Sub

**Problem is: when I click on the tab, the sheet is displayed in the
background behind the prompt. How can I make it so that it doesn't display at
all until the password is successfully entered?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top