Consulting

Results 1 to 8 of 8

Thread: Solved: Stop user accessing worksheet "Administartor"

  1. #1
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    294
    Location

    Solved: Stop user accessing worksheet "Administartor"

    see attached workbook
    Hi

    Need to stop any user if they try and click the worksheet "Administrator" (tab) a messagebox appears with warning "Cannot Access Administrator Worksheet, You Do Not Have Access Right"......

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why not just hide it?
    ____________________________________________
    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 Tutor
    Joined
    Aug 2007
    Posts
    294
    Location
    no tried that keep on gettiung around that............so just thought code might work this time......with a warning thrown in...

  4. #4
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    Put the following code into a new module in your workbook. It will get the current PC UserName. You can then compare the UserName to either a single value (if only one person has 'Admin rights', or you can compare it to an array of people who have Admin rights).

    You can use this value to give or deny access to your worksheet.

    [vba]Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

    Function fOSUserName() As String
    Environ$ ("Username")

    Dim lngLen As Long, lngX As Long
    Dim strUserName As String

    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)
    If (lngX > 0) Then
    fOSUserName = left$(strUserName, lngLen - 1)
    Else
    fOSUserName = vbNullString
    End If
    End Function[/vba]

    Now, go to the code window for your Admin worksheet and use the Worksheet_Activate event to evaluate the username that is trying to access the worksheet. Example:
    [vba]Sub Worksheet_Activate()
    If fOSUserName <> 'Insert your UserName here as string Then
    MsgBox "You are not authorized to view this worksheet."
    End If
    End Sub[/vba]

    I'm not completely familiar with Excel, so I don't know the method to change the active worskheet back to Sheet1, but that's pretty much what you need to do.

    HTH
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  5. #5
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    The problem is, that will break if the user disables macros. The best you can do if you want to keep it hidden is to go into the vb project and make the sheet VeryHidden (so it doesn't show up in the "unhide sheets" list. Change the codename and worksheet name to something less obvious, and the password protect the vbproject. That will at least stop users who don't know code.

    Additionally I would share the workbook and turn on change tracking (because even if they clear the log, then you have an empty log to clue you in).

    Out of curiousity what settings are on the admin tab that you trying to protect?
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You would also need to add code to force enabling of macros. There are some examples in the KB.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    294
    Location
    thanks for teh feedback......great works better then my own verison.......

  8. #8
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Another thing to consider is to password protect the workbook. This cause the data to be non-user readable if they decide to open the workbook in notepad, etc.
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

Posting Permissions

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