Private Sub Workbook_SheetChange Not Triggering - why?

pwyller

New Member
Joined
Sep 30, 2016
Messages
14
Why doesn't this work/trigger?

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, _
 ByVal Source As Range)


Application.EnableEvents = True
Application.ScreenUpdating = False


    If Target.Address <> "$A$1" Then Exit Sub
        Application.Columns("D:BP").Select
        Application.Selection.EntireColumn.Hidden = False
        If Target.Value = "1" Then
            Application.Columns("D:J").Select
            Application.Selection.EntireColumn.Hidden = True
        End If
        If Target.Value = "2" Then
            Application.Columns("D:AM").Select
            Application.Selection.EntireColumn.Hidden = True
        End If
        If Target.Value = "3" Then
            Application.Columns("D:Q").Select
            Application.Selection.EntireColumn.Hidden = True
        End If
        If Target.Value = "4" Then
            Application.Columns("D:X").Select
            Application.Selection.EntireColumn.Hidden = True
        End If
        If Target.Value = "5" Then
            Application.Columns("D:BP").Select
            Application.Selection.EntireColumn.Hidden = False
        End If
        If Target.Value = "6" Then
            Application.Columns("D:AE").Select
            Application.Selection.EntireColumn.Hidden = True
        End If
        If Target.Value = "7" Then
            Application.Columns("D:AS").Select
            Application.Selection.EntireColumn.Hidden = True
        End If
        If Target.Value = "8" Then
            Application.Columns("D:BA").Select
            Application.Selection.EntireColumn.Hidden = True
        End If
        If Target.Value = "9" Then
            Application.Columns("D:BH").Select
            Application.Selection.EntireColumn.Hidden = True
        End If
    End If


'Application.EnableEvents = True
Application.ScreenUpdating = True


End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Do you have it stored in the ThisWorkbook sheet?

Also, you have "ByVal Source as Range" in the subroutine definition, and you use "Target" in the body of the subroutine. Also, is EnableEvents True before the routine executes? The more usual sequence of events is to disable EnableEvents at the start of the routine, and re-enable it at the end.
 
Upvote 0
Every time I had this issue, Application.EnableEvents was set to False (usually after prematurily exiting a macro).

On a sidenote, I would advise you not to use .Select. The code below would work without having to select anything, which is considered bad practice.

Code:
[COLOR=#333333]Application.[/COLOR][COLOR=#333333]Columns("D:BA")[/COLOR][COLOR=#333333].EntireColumn.Hidden[/COLOR]

Also, disable Events before going through the Worksheet_Change event. If the code within the event changes anything on that worksheet, it will trigger itself, resulting in an infinite loop.
 
Upvote 0
Try:
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
    Application.EnableEvents = True
    Application.ScreenUpdating = False
    Application.Columns("D:BP").EntireColumn.Hidden = False
    Select Case Target.Value
        Case Is = 1
            Columns("D:J").EntireColumn.Hidden = True
        Case Is = 2
            Columns("D:AM").EntireColumn.Hidden = True
        Case Is = 3
            Columns("D:Q").EntireColumn.Hidden = True
        Case Is = 4
            Columns("D:X").EntireColumn.Hidden = True
        Case Is = 5
            Columns("D:BP").EntireColumn.Hidden = True
        Case Is = 6
            Columns("D:AE").EntireColumn.Hidden = True
        Case Is = 7
            Columns("D:AS").EntireColumn.Hidden = True
        Case Is = 8
            Columns("D:BA").EntireColumn.Hidden = True
        Case Is = 9
            Columns("D:BH").EntireColumn.Hidden = True
        End Select
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top