username access to different worksheet VBA

cilantro00

New Member
Joined
Aug 8, 2024
Messages
2
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
  2. MacOS
hi i would need help with a VBA code that have very specific properties.
im working on a workbook where each user should only have access to view and edit the sheet assigned to their username. Only when the username is "ADMIN" that all sheets are unhidden. i tried to set up an inputbox for users to enter their username, which would then unhide the corresponding sheet. However, the set up doesn't seem to work when there a more than one user accessing the workbook simultaneously. The codes exposes all unhidden sheets to all users instead of unhidding one sheet per user. i tried to set it up so that the sheet closes itself after 5 minutes but it does not seem to work properly with multiple users on it.

VBA Code:
Option Explicit


 


Dim username As String
Dim foundSheet As Boolean
Dim ws As Worksheet
Dim activeSheetName As String


 


Sub HideSheets()
   Dim ws As Worksheet
   activeSheetName = ActiveSheet.Name ' Store the name of the currently active sheet
   
   For Each ws In ThisWorkbook.Sheets
       If ws.Visible = xlSheetVisible And ws.Name <> activeSheetName Then
           ws.Visible = xlSheetVeryHidden
       End If
   Next ws
End Sub


 


Sub ShowSheets()
   Dim ws As Worksheet
   For Each ws In ThisWorkbook.Sheets
       ws.Visible = xlSheetVisible
   Next ws
End Sub


 


Sub User()
   Dim ws As Worksheet


 


    foundSheet = False
   
   Do
       username = InputBox("Please enter username:", "Username")
       If username = "" Then
           MsgBox "No username entered. Exiting.", vbExclamation
           Exit Sub
       End If
       username = UCase(username)


 


        If username = "ADMIN" Then
           Call ShowSheets
           Exit Sub
       End If


 


        For Each ws In ThisWorkbook.Worksheets
           If UCase(ws.Name) = username Then
               ws.Visible = xlSheetVisible
               foundSheet = True
               Exit For
           End If
       Next ws


 


        If Not foundSheet Then
           MsgBox "No sheet found with the name: " & username, vbExclamation
       End If
   Loop Until foundSheet = True


 


    If foundSheet Then
       Application.OnTime Now + TimeValue("00:00:05"), "HideSheets"
   End If
End Sub


 


Sub Appeler1()
   Call HideSheets
   Call User
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
If I happen to know someone else's username I can view his/her tab? Doesn't sound very secure.
 
Upvote 0
VBA Code:
Environ("Username")
should give you the username as the user is logged on with.
 
Upvote 0
You could add this code to the ThisWorkbook module:

VBA Code:
Private Sub Workbook_Open()
    Dim sh As Worksheet
    Dim un As String
    un = Environ("username")
    For Each sh In ThisWorkbook.Worksheets
        If sh.Name = un Then
            sh.Activate
            Exit For
        End If
    Next
End Sub

Private Sub Workbook_SheetActivate(ByVal sh As Object)
    Workbook_Open
End Sub
 
Upvote 0
You could add this code to the ThisWorkbook module:

VBA Code:
Private Sub Workbook_Open()
    Dim sh As Worksheet
    Dim un As String
    un = Environ("username")
    For Each sh In ThisWorkbook.Worksheets
        If sh.Name = un Then
            sh.Activate
            Exit For
        End If
    Next
End Sub

Private Sub Workbook_SheetActivate(ByVal sh As Object)
    Workbook_Open
End Sub
i’m still new to VBA and coding in general so i’m a bit lost about how Environ works or how it’s set up in general. I tried putting it in ThisWorkbook but when i open the workbook, it shows me other sheets that are assigned to other usernames.
 
Upvote 0

Forum statistics

Threads
1,224,862
Messages
6,181,464
Members
453,044
Latest member
rgbenson1

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