Windows Environ restricting access

jaypatel

Active Member
Joined
Nov 25, 2002
Messages
389
Hi

I have searched high and low for this solution...

If I have 4 sheets in a workbook, named Summary, Anne, Jay, Simon.

I am looking for Anne to view only her sheet, Simon to view only his sheet, and Jay to view all....
(Their names could be in cell A1 of each of their sheets).
How would I do this with "Environ" property?

Thanks

Jay
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Assuming
- Anne & Simon are only allowed to see their own sheet and not the Summary
- Everybody's sheet name is the same as their environ user name, apart from perhaps capitalisation

then try this with a copy of your workbook.

1. Add a new sheet that everybody is allowed to see. Could be blank. I've named that sheet 'All'. Adjust code if you want to use a different name.
2. Add the following two codes to the ThisWorkbook module in the vba window.

Rich (BB code):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim ws As Worksheet
    
    For Each ws In Worksheets
      If ws.Name <> "All" Then ws.Visible = xlSheetVeryHidden
    Next ws
    ThisWorkbook.Save
End Sub

Private Sub Workbook_Open()
    Dim user As String
    Dim ws As Worksheet
    
    user = LCase(Environ("username"))
    Select Case user
      Case "jay"
        For Each ws In Worksheets
          ws.Visible = True
        Next ws
      Case "anne", "simon"
        Sheets(user).Visible = True
      Case Else
        'Unhide nothing
    End Select
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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