简体   繁体   中英

Excel macro not triggering on cell change

I have a macro which previously triggered on a cell change but for some reason now it fails to trigger. I'm not sure what has changed to have caused this. All the macro has to do is refresh 4 pivots, which can be done manually no problem.

I've tried different ways of setting up a macro to trigger on a cell change, however they all fail to trigger on cell change.

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim KeyCells As Range

    Set KeyCells = Range("V76:W76")

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

    Sheets("NATFLOW").PivotTables("PivotTableA2").PivotCache.Refresh
    Sheets("NATFLOW").PivotTables("PivotTableB2").PivotCache.Refresh

    Sheets("NATTABLE").PivotTables("PivotTableAlpha2").PivotCache.Refresh
    Sheets("NATTABLE").PivotTables("PivotTableBeta2").PivotCache.Refresh

    End If

End Sub

The outcome is that nothing happens, including no error messages.

If nothing happends, including no error messages, so is due to this Application.Intersect(KeyCells, Range(Target.Address)) equals nothing .

Try to run step by step and check if the value of Application.Intersect(KeyCells, Range(Target.Address))

Try:

Option Explicit

'Method 1 - Specific sheet, specific objects
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("V76:W76")) Is Nothing And Target.Count = 1 Then

        With Sheets("NATFLOW")

            .PivotTables("PivotTableA2").PivotCache.Refresh
            .PivotTables("PivotTableB2").PivotCache.Refresh

            .PivotTables("PivotTableAlpha2").PivotCache.Refresh
            .PivotTables("PivotTableBeta2").PivotCache.Refresh

        End With

    End If

End Sub

'Method 2 - Refresh everyting from this workbook
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("V76:W76")) Is Nothing And Target.Count = 1 Then

        ThisWorkbook.RefreshAll

    End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM