Results 1 to 3 of 3

Thread: Password Access for a sheet

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    5

    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
    Last edited by si_the_geek; Sep 28th, 2010 at 09:14 AM. Reason: added Code tags

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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)

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width