Results 1 to 17 of 17

Thread: Hide Worksheets dependant on domain name

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #17
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,888
    Location
    1. Add a BeforeClose event to ThisWorkbook

    Option Explicit
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        sUserID = "Unknown"
        ShowRightWorksheets
    End Sub
    
    Private Sub Workbook_Open()
        Set wsUsers = Worksheets("Users")
        Set rUsers = wsUsers.Cells(1, 1).CurrentRegion
        Set wsIntro = Worksheets("Sheet1")
        
        
        sUserID = Environ("UserName")
        sUserName = Application.UserName
        
        ShowRightWorksheets
        
        wsIntro.Select
        
    End Sub

    2. I removed the Intro idea, but there were a few compatibility changes to the other macro, so

    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
        
        Application.ScreenUpdating = False
        '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
            
            ws.Visible = xlSheetVisible
            
            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
                End If
            
            'if WS NOT in list, then VeryHide it
            Else
                ws.Visible = xlSheetVeryHidden
            End If
        Next
        Application.ScreenUpdating = True
    End Sub

    3. I made some changes to the User database worksheet to always have Sheet1 visible
    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
  •