Results 1 to 17 of 17

Thread: Hide Worksheets dependant on domain name

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,887
    Location
    These things a tendency to grow over time - new users, more worksheets, changed worksheets, removed users, etc.

    I'd suggest a little 'data base' worksheet that can be more easily maintained

    Capture.JPG


    For 'Admin' you can replace that with your own UserID and when you open it you can see all the worksheets, including 'Users'

    You can .Protect the Intro worksheet if you want, or make it really pretty and wow the boss

    Option Explicit
    Private Sub Workbook_Open()
        Application.ScreenUpdating = False
        
        Set wsUsers = Worksheets("Users")
        Set rUsers = wsUsers.Cells(1, 1).CurrentRegion
        Set wsIntro = Worksheets("Intro")
        
        sUserID = Environ("UserName")
        sUserName = Application.UserName
        wsIntro.Shapes("IntroBox").TextFrame2.TextRange.Text = "Hello" & vbLf & sUserName
        
        ShowRightWorksheets
        
        wsIntro.Select
        
        Application.ScreenUpdating = True
        
    End Sub

    Option Explicit
    Option Private Module
    Public wsUsers As Worksheet, wsIntro As Worksheet
    Public sUserID As String, sUserName As String
    Public rUsers As Range
    
    Sub ShowRightWorksheets()
        Dim iUser As Long, iWS As Long
        Dim ws As Worksheet
        'if user not in list, use USER=Unknown in col B
        iUser = 2
        On Error Resume Next
        iUser = Application.WorksheetFunction.Match(sUserID, rUsers.Rows(1), 0)
        On Error GoTo 0
        'find the WS row
        For Each ws In ActiveWorkbook.Worksheets
            iWS = 0
            On Error Resume Next
            iWS = Application.WorksheetFunction.Match(ws.Name, rUsers.Columns(1), 0)
            On Error GoTo 0
            
            'if the WS is in the list
            If iWS > 0 Then
                'if the row/col is empty then VeryHide the WS
                If Len(rUsers.Cells(iWS, iUser).Value) = 0 Then
                    ws.Visible = xlSheetVeryHidden
                'otherwise show the WS
                Else
                    ws.Visible = xlSheetVisible
                End If
            
            'if WS NOT in list, then VeryHide it
            Else
                ws.Visible = xlSheetVeryHidden
            End If
        Next
    End Sub
    'super secret hidden macro that you need to know the name of
    'so that you can type it in with Alt-F8 - Run Macro
    Sub SuperSecret()
        Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
            ws.Visible = xlSheetVisible
        Next
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •