Password Access for a sheet
I am new to writing VBA and need to restrict access to a sheet in a workbook. I have macro that assigns a password to the sheet, but it seems that the password isnt defined as anything entered will unlock the password. Can someone please look at my code and identify where I am missing assigning the password a value?
Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim strPass As String
correct_pass_given = 0
Dim lCount, number_of_tries_allowed As Long
Set hide_sheet = Sheet11 ' Give the name of the sheet to hide (This is not the Sheet Tab name but the one that shows outside the bracket.)
number_of_tries_allowed = 3 'this is to restrict the number of tries at one go
'MsgBox correct_pass_given
If ActiveSheet.Name <> "ContractorInformation" Or correct_pass_given = 1 Then
Else
hide_sheet.Columns.Hidden = True
'Allow 3 attempts at password
For lCount = 1 To number_of_tries_allowed
strPass = InputBox(Prompt:="Password Please", Title:="PASSWORD REQUIRED")
If strPass = vbNullString Then 'Cancelled
MsgBox "Password incorrect", vbCritical, "Message"
Else: correct_pass_given = 0 'Correct Password
Exit For
End If
Next lCount
If lCount = number_of_tries_allowed + 1 Then 'They use up their 3 attempts
Exit Sub
Else 'Allow viewing
hide_sheet.Columns.Hidden = False
End If
End If
End Sub
Re: Password Access for a sheet
Thread moved from 'VB6' forum to the 'Office Development/VBA' forum (while VBA and VB6 have some similarities, they are not the same thing)
Re: Password Access for a sheet
The only thing you are checking for is whether the password entered is null.
If not, then it goes right through.
You need to be checking if what is being entered = 'proper password'