Consulting

Results 1 to 8 of 8

Thread: Securing a sheet with VBA in excel

  1. #1
    VBAX Newbie
    Joined
    Feb 2012
    Posts
    4
    Location

    Securing a sheet with VBA in excel

    Dear all,

    I am creating a new project with a very tight deadline and I am having trouble with some crucial details. I need to secure some of the sheets in my workbook with a password so that they can be accessed only by certain people with a specific password. The data also must stay hidden unless the correct password is given. I searched the internet and found an VBA code that is fitting my needs. There is a problem however. As I am an beginner with VBA, I am not able to modify the code so that it is fully working. Everything works fine, but the main problem is that the data are shown even if an uncorrect password is inserted. I don't have a clue how to fix this. Not being able to secure some of the lists in the way described above will mean a big problem for me. Could somebody please help me? Every suggestion is much appreciated!

    My modified code is:
    I need to secure "Sheet2" which is named e. g. "Effectifs" with the password "Heslo123" and the user has 3 attemps


    Dim correct_pass_given As Integer
    Dim hide_sheet As Worksheet


    Private Sub Workbook_Open()
    correct_pass_given = Heslo123
    'Set hide_sheet = Sheet2 '
    Sheet1.Select
    End Sub

    Private Sub Workbook_SheetActivate(ByVal Sh As Object)

    Dim strPass As String
    Dim lCount, number_of_tries_allowed As Long
    Set hide_sheet = Sheet2 '
    number_of_tries_allowed = 3 '
    'MsgBox correct_pass_given

    If ActiveSheet.Name <> "Effectifs" 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 = 1 'Correct Password
    Exit For
    End If
    Next lCount
    If lCount = number_of_tries_allowed + 1 Then '
    Exit Sub
    Else 'Allow viewing
    hide_sheet.Columns.Hidden = False
    End If
    End If
    End Sub

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If you want to hide the data, hide the worksheet.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Feb 2012
    Posts
    4
    Location
    Hiding the sheet is not an option, I need it to be accessible for the users. E. g. for HR will be Sheet1 available after inserting a password, for Finance department Sheet2, for PR sheet 3 etc.

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This code assumes that there is a worksheet Main, that everybody is allowed to see. It requires that BobsPassword be entered to view the worksheet BobSheet. Similarly, MarysPassword is requried to see MarySheet.


    [VBA]' in ThisWorkbook code module

    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Dim RequiredPassword As String, desiredSheet As Worksheet
    Set desiredSheet = Sh

    Select Case LCase(Sh.Name)
    Case "bobsheet"
    RequiredPassword = "BobsPassword"
    Case "marysheet"
    RequiredPassword = "MarysPassword"
    Case Else
    RequiredPassword = vbNullString
    End Select

    If RequiredPassword <> vbNullString Then
    On Error GoTo ErrorOut
    Application.EnableEvents = False
    Sheets("Main").Activate
    If Application.InputBox("Enter the password", Default:=RequiredPassword, Type:=2) = RequiredPassword Then
    desiredSheet.Activate
    Else
    MsgBox "Wrong Password"
    End If
    End If

    ErrorOut:
    Application.EnableEvents = True
    End Sub

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Sheets("Main").Activate
    End Sub

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Sheets("Main").Activate
    End Sub[/VBA]
    Attached Files Attached Files

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Prospero
    Hiding the sheet is not an option, I need it to be accessible for the users. E. g. for HR will be Sheet1 available after inserting a password, for Finance department Sheet2, for PR sheet 3 etc.
    So hide them all and have a navigation sheet where the user inputs a password
    or a form, and unhide the appropriate sheet.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Newbie
    Joined
    Feb 2012
    Posts
    4
    Location
    To mikerickson: Thank you so much! This works like a charm! Absolutely perfect.

    xld: I would like to thank you also very much, I will definitely try out what you are suggesting!

    BR,

    Petr.

  7. #7
    VBAX Newbie
    Joined
    Feb 2012
    Posts
    4
    Location
    Quote Originally Posted by mikerickson
    This code assumes that there is a worksheet Main, that everybody is allowed to see. It requires that BobsPassword be entered to view the worksheet BobSheet. Similarly, MarysPassword is requried to see MarySheet.


    [vba]' in ThisWorkbook code module

    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Dim RequiredPassword As String, desiredSheet As Worksheet
    Set desiredSheet = Sh

    Select Case LCase(Sh.Name)
    Case "bobsheet"
    RequiredPassword = "BobsPassword"
    Case "marysheet"
    RequiredPassword = "MarysPassword"
    Case Else
    RequiredPassword = vbNullString
    End Select

    If RequiredPassword <> vbNullString Then
    On Error GoTo ErrorOut
    Application.EnableEvents = False
    Sheets("Main").Activate
    If Application.InputBox("Enter the password", Default:=RequiredPassword, Type:=2) = RequiredPassword Then
    desiredSheet.Activate
    Else
    MsgBox "Wrong Password"
    End If
    End If

    ErrorOut:
    Application.EnableEvents = True
    End Sub

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Sheets("Main").Activate
    End Sub

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Sheets("Main").Activate
    End Sub[/vba]
    There is one problem with the solution you suggested - when I go e. g. to the sheet "BobSheet" and I click again on "BobSheet" than I am able to view all the data in BobSheet without actually entering the password. Is there any way arround this e. g. by a command to hide the colums before the password is inserted?

  8. #8
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    I don't understand the problem.

    The entirety of BobsSheet should be not viewable except with entering Bobs passoword.

    Are you trying to hide portions of a worksheet from not-Bob, but let not-Bob view the other portions?

Posting Permissions

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