Consulting

Results 1 to 2 of 2

Thread: VBA code to apply to whole workbook

  1. #1
    VBAX Newbie
    Joined
    Jul 2016
    Posts
    1
    Location

    VBA code to apply to whole workbook

    Hi

    I need to apply this VBA code to my whole workbook, can anyone help please?

    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
        If Target.Address = "$F$6" Then
            Application.EnableEvents = False
                Columns("G:I").EntireColumn.Hidden = False
                If Range("F6").Value = 1 Then
                    Columns("G:I").EntireColumn.Hidden = True
                ElseIf Range("F6").Value = 2 Then
                    Columns("H:I").EntireColumn.Hidden = True
               ElseIf Range("F6").Value = 3 Then
                   Columns("I:I").EntireColumn.Hidden = True
                End If
            Application.EnableEvents = True
        End If
    End Sub
    Thanks in advance

    Chris

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    One interpretation of your request
    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
        If Target.Address = "$F$6" Then
            Application.EnableEvents = False
            For Each Sh In Worksheets
            With Sh
                .Columns("G:I").EntireColumn.Hidden = False
                If Range("F6").Value = 1 Then
                    .Columns("G:I").EntireColumn.Hidden = True
                ElseIf Range("F6").Value = 2 Then
                    .Columns("H:I").EntireColumn.Hidden = True
                ElseIf Range("F6").Value = 3 Then
                   .Columns("I:I").EntireColumn.Hidden = True
                End If
            End With
            Next Sh
            Application.EnableEvents = True
        End If
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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