我不希望我的 VBA 代码更改工作表

问题描述

因此,每次在主工作表(仪表板)中进行更改时,我都尝试使用代码简单地刷新数据透视表。

因为我需要保持干净,所以我有另一张工作表,其中包含所有信息,名为 historic_var

问题是,当我运行代码时,它运行良好,但它更改为没有用的工作表(除非保证信息安全)。我能做什么?已经试过了application.ScreenUpdating = False

当前代码为:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column <= 14 Then
        Sheets("historic_var").Select
        ActiveSheet.Pivottables("Tabela dinâmica1").PivotCache.Refresh
    End If
End Sub

解决方法

您可以在不更改工作表的情况下更新表格。试试这个...

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Column <= 14 Then
    
    Sheets("historic_var").PivotTables("Tabela dinâmica1").PivotCache.Refresh
    End If

End Sub