VBA - Sheets filter after user on Workbook Open

Hellyworld

New Member
Joined
May 3, 2017
Messages
6
Hello,

I need some help with a code on Excel 2010 that is not working as expected.

How it must work:
All the sheets (43 in total) except one are on visibility 2 - xlSheetVeryHidden and the code will make visible only the necessary sheets depending on the Application.UserName. With this code I tried to filter the access on sheets of different users, power user and admins.

What is not working:
It seems that the first condition is executed but not the rest of them and all the sheets are becoming visible. If i will keep only the last condition each user will see only his tab.
Code:
Private Sub Workbook_Open()


For Each ws In ActiveWorkbook.Worksheets
        
        If Application.UserName = "admin" Then
            ws.Visible = xlSheetVisible
            
        ElseIf Application.UserName = "power user" Then
            ws.Visible = xlSheetVisible
            
        ElseIf InStr(ws.Name, Application.UserName) > 0 Then
            ws.Visible = xlSheetVisible
          
        End If
        Next ws

End Sub

If you need more details please let me know.
 

Forum statistics

Threads
1,226,834
Messages
6,193,216
Members
453,779
Latest member
C_Rules

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