How to restrict access to worksheets per user

L

Legacy 374719

Guest
Hi guys, I was wondering whether you could help, I have a workbook with 10 different sheets, that I share with 9 other people, First sheet is what everyone can see, then each has their own work sheet they work with. I need to find a way to restrict access and visibility of other sheets. so that user 1 can see sheet 2 only, user 2 can see sheet 3 only and so on. And lastly I'd like a super user, myself, who can see all sheets. Can anyone advice how to achieve this with VBA?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Thanks for the super fast reply Gallen. Would you help me in replacing the Application.Username with Environ ("username")?
I am getting error message.
 

Attachments

  • Code.png
    Code.png
    193.3 KB · Views: 49
Upvote 0
Thanks for the super fast reply Gallen. Would you help me in replacing the Application.Username with Environ ("username")?
I am getting error message.
You've misunderstood how Environ works. Easily done.
It needs to be
VBA Code:
If Environ("Username") = "AAAAA" then

Google "VBA Environ" for full list
 
Upvote 0
You've misunderstood how Environ works. Easily done.
It needs to be
VBA Code:
If Environ("Username") = "AAAAA" then

Google "VBA Environ" for full list
Thanks Gallen, I realized my silly mistake immediately after sending your message but felt embarrassed. It's working like a charm and I am going to try the same for the non-power user to see if it works. So sorry for being a pain but let you know my result.
Kindest,
Taha
 
Upvote 0
To achieve exactly what you asked you could put this code in the 'ThisWorkbook' module of the Code section:

Code:
Private Sub Workbook_Open()
    ShowSheet
End Sub


Sub ShowSheet()
    Dim ws As Worksheet
    Dim wsAllowed As Worksheet
  
    'If SuperUser, show all:
    If Application.UserName = "SuperUserName" Then 'insert Super User's name here
        For Each ws In Worksheets
           ws.Visible = xlSheetVisible
        Next
        Exit Sub
    End If
  
    'If not a super user, only show the associated sheet
    Set wsAllowed = GetAllowedSheet
    wsAllowed.Visible = xlSheetVisible 'make sure the sheet is visible before hiding the others
  
    'Hide all sheets that user isn't permitted to see.
    For Each ws In Worksheets
        If ws.Name <> wsAllowed.Name Then ws.Visible = xlSheetHidden
    Next
  
End Sub


Function GetAllowedSheet() As Worksheet
    'Set the sheet each user is allowed to use here.
  
    'adapt the case statements for correct users and correct sheets to display
    Select Case Application.UserName
        Case "User 1"
           Set GetAllowedSheet = Sheets("Sheet2")
        Case "User 2"
           Set GetAllowedSheet = Sheets("Sheet1")
        Case Else
            'If code gets here then User Name is unhandled.
    End Select
End Function

Naturally it would need modifying to have the correct usernames and to handle what would happen if the username is unknown.


Having said that I'd keep all sheets visible but in the 'WorkSheet_Change' event of each sheet I'd check the username and if they aren't allowed to make any, pop up a message box and undo the change.

Hello again Gallen,
Would you please advise as to how I can possible change
If Environ("Username") = "AAAAA" then

Hello Gallen,
Could you please advise why I am getting an error?
 

Attachments

  • Error.png
    Error.png
    214.9 KB · Views: 16
Upvote 0
Hello again Gallen,
Would you please advise as to how I can possible change


Hello Gallen,
Could you please advise why I am getting an error?
Can't have if statement within a select statement
You need to search "how to use Select statement VBA"

There's many examples online so pointless me adding another.
 
Upvote 0
With Sheet1 as the sheet everyone can see.
I would suggest that on all of the other, individual sheets, you put a Rectangle.
For each of those rectangles, right click and Edit the Alt text so that the Alt Text is the password for that sheet.
Then put this in the ThisWorkbook code module
VBA Code:
' in ThisWorkbook code module

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

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Me.Sheets("Sheet1").Activate
End Sub

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Dim oneSheet As Worksheet, aRange As Range
    For Each oneSheet In Me.Worksheets
        If oneSheet.Name <> "Sheet1" Then
            With oneSheet
                Set aRange = Range(.Range("A1"), .UsedRange)
                If .Shapes.Count > 0 Then
                    .Unprotect
                    With .Shapes(1)
                        .OnAction = "UserTest"
                        .Width = aRange.Width
                        .Height = aRange.Height
                        .Top = 0
                        .Left = 0
                        .Visible = msoCTrue
                    End With
                    .Protect
                End If
            End With
        End If
    Next oneSheet
End Sub

And this code in a normal module
VBA Code:
Sub UserTest()
    Dim aShape As Shape
    With ActiveSheet.Shapes(1)
        If Application.InputBox("Enter Your Password", Type:=2) = .AlternativeText Then
            ActiveSheet.Unprotect
            .Visible = False
        Else
            MsgBox "wrong password"
        End If
    End With
End Sub
 
Upvote 0
@Tahas
While we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.

Please supply links to all other sites where you have asked this question.
 
Upvote 0
Hey Gallen,
I was looking for the solution and found your post. This code is working very good.
One issue I am facing. When the workbook is opened in excel online, all the sheets are visible and users can view all the sheets.
Any work around for this issue ?
Regards,
Arjun Mehta
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,858
Members
449,194
Latest member
HellScout

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top