简体   繁体   English

Excel宏未在单元格更改时触发

[英]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. 宏所需要做的就是刷新4个枢轴,可以手动完成这没问题。

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 . 如果什么都没发生,包括没有错误消息,则是由于此Application.Intersect(KeyCells, Range(Target.Address))等于nothing

Try to run step by step and check if the value of Application.Intersect(KeyCells, Range(Target.Address)) 尝试逐步运行,并检查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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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