PDA

View Full Version : Solved: Stop user accessing worksheet "Administartor"



Pete
07-21-2008, 12:15 PM
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"......

Bob Phillips
07-21-2008, 12:33 PM
Why not just hide it?

Pete
07-21-2008, 12:35 PM
no tried that keep on gettiung around that............so just thought code might work this time......with a warning thrown in...

CreganTur
07-21-2008, 01:23 PM
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.

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

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:
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

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 :thumb

Oorang
07-21-2008, 01:31 PM
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?

mdmackillop
07-21-2008, 01:33 PM
You would also need to add code to force enabling of macros. There are some examples in the KB.

Pete
07-21-2008, 11:33 PM
thanks for teh feedback......great works better then my own verison.......

Oorang
07-22-2008, 08:45 AM
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.