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


Reply With Quote
