选择工作表时,在工作表上刷新两个数据透视表

时间:2018-04-18 18:29:59

标签: excel vba excel-vba

我在工作表上有两个数据透视表,我希望在工作表打开时自动刷新。我已将该宏放在该表中。但是,我在这行“oPivot.RefreshTable”

上收到错误
Private Sub Worksheet_Activate()

Dim oSheet As Worksheet
Dim oPivot As PivotTable

Set oSheet = ActiveSheet

For Each oPivot In oSheet.PivotTables
    oPivot.RefreshTable
Next oPivot

End Sub

1 个答案:

答案 0 :(得分:0)

此方法有效:

Private Sub Worksheet_Activate()

Dim oPivot As PivotTable
Dim xlsheet As Worksheet

Set xlsheet = ActiveSheet

For Each oPivot In xlsheet.PivotTables
    oPivot.PivotCache.Refresh
Next oPivot 

End Sub