Forum Discussion

Ghalder's avatar
Ghalder
Copper Contributor
Feb 05, 2024

Assigning sheets to user in online document

Hello.

i am in desperate need of a Vba code to limit ussers Access to an online sheet. 

I have a worksheet With 24 sheets. the stores are putting in somme info for ther own store. and ther have been problems users not using the right sheet.  so i want to hide all sheets exept The One each user gets assigned. 

 

i have been working on a code but i can't seem to get it to work 

Sub HideAllSheets()
' Skjul alle ark for alle brukere
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If ws.Name <> "NyttArk101" Then
' Bruk xlSheetHidden for å skjule arkene
ws.Visible = xlSheetHidden
End If
Next ws
End Sub

Private Sub Workbook_Open()
Dim currentUserName As String

' Hent e-postadressen fra Microsoft-kontoen
currentUserName = Application.Session.CurrentUser.Address

' Skjul alle ark
HideAllSheets

' Vis kun det spesifikke arket for hver bruker
Select Case UCase(currentUserName)
Case "email address removed for privacy reasons"
ShowSpecificSheet "NyttArk101"

Case "email address removed for privacy reasons"
ShowSpecificSheet "NyttArk102"

Case "email address removed for privacy reasons"
ShowSpecificSheet "NyttArk103"

' Legg til saker for andre brukernavn og ark

End Select
End Sub

Sub ShowSpecificSheet(sheetName As String)
' Vis kun det spesifikke arket
Worksheets(sheetName).Visible = xlSheetVisible
End Sub

 

2 Replies

  • JKPieterse's avatar
    JKPieterse
    Silver Contributor

    Ghalder You have probably found out by now that you cannot hide all sheets. So the trick is to first show the sheet that you want shown and then hide all others:

     

    Private Sub Workbook_Open()
        Dim currentUserName As String
    
        ' Hent e-postadressen fra Microsoft-kontoen
        currentUserName = Application.Session.CurrentUser.Address
    
        ' Vis kun det spesifikke arket for hver bruker
        Select Case UCase(currentUserName)
        Case "email address removed for privacy reasons"
            HideAllSheetsExcept "NyttArk101"
        Case "email address removed for privacy reasons"
            HideAllSheetsExcept "NyttArk102"
        Case "email address removed for privacy reasons"
            HideAllSheetsExcept "NyttArk103"
            ' Legg til saker for andre brukernavn og ark
        End Select
    End Sub
    
    Sub HideAllSheetsExcept(sheetName As String)
        Dim sht As Worksheet
        ' Vis kun det spesifikke arket
        Worksheets(sheetName).Visible = xlSheetVisible
        For Each sht In Worksheets
            If sht.Name <> sheetName Then
                sht.Visible = xlSheetHidden
            End If
        Next
    End Sub
    • Ghalder's avatar
      Ghalder
      Copper Contributor
      thanks for the help.
      I havent had the time to future look into it afther i posted, but removng hide all sheets make senes .

Resources