MS EXCEL Code will not work with "Active Sheets" only the current sheet.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anoble1
    New Member
    • Jul 2008
    • 246

    MS EXCEL Code will not work with "Active Sheets" only the current sheet.

    I have an excel sheet with 5 worksheets inside it (5 tabs). I have 3 columns that I want to hide unless their NTID is asnoble. If they arn't me then hide the 3 coluns on all the sheets. The way it happens now is right when it comes up, the code works on the first sheet. Then when you click another sheet it stops working. I have this code on all the tabs in code, also a module. It just doesn't work whenever you click another sheet.

    I put my name in their as an example :)
    Please help

    Here is my code:
    Code:
    Sub Auto_Open()
    '
    ' Auto_Open Macro
    ' Macro recorded 6/4/2010 by Joseph D. Marcrum, III
    '
    
    '
    
    If Environ("username") <> "asnoble" Then
        Columns("I").Hidden = True
        Columns("J").Hidden = True
        Columns("K").Hidden = True
    End If
    
    End Sub
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by anoble1
    I have an excel sheet with 5 worksheets inside it (5 tabs). I have 3 columns that I want to hide unless their NTID is asnoble. If they arn't me then hide the 3 coluns on all the sheets. The way it happens now is right when it comes up, the code works on the first sheet. Then when you click another sheet it stops working. I have this code on all the tabs in code, also a module. It just doesn't work whenever you click another sheet.

    I put my name in their as an example :)
    Please help

    Here is my code:
    Code:
    Sub Auto_Open()
    '
    ' Auto_Open Macro
    ' Macro recorded 6/4/2010 by Joseph D. Marcrum, III
    '
    
    '
    
    If Environ("username") <> "asnoble" Then
        Columns("I").Hidden = True
        Columns("J").Hidden = True
        Columns("K").Hidden = True
    End If
    
    End Sub
    Code:
    Dim sht As Excel.Worksheet
    
    If Environ("username") <> "asnoble" Then
      For Each sht In ActiveWorkbook.Worksheets
        sht.Columns("I:K").Hidden = True
      Next
    End If

    Comment

    Working...