我正在尝试编写一个宏,在将新数据放入原始表时更新我的数据透视表。我继续收到错误,不知道如何解决它。这是我到目前为止所拥有的。
Sub UpdatePivotTableRange()
Dim Data_Sheet As Worksheet
Dim Pivot_Sheet As Worksheet
Dim StartPoint As Range
Dim PivotName As String
Dim NewRange As String
Dim LastCol As Long
Dim lastRow As Long
Set Data_Sheet = ThisWorkbook.Worksheets("Data insert")
Set Pivot_Sheet = ThisWorkbook.Worksheets("CC_Users")
PivotName = PivotTable6 Data_Sheet.Activate
Set StartPoint = Data_Sheet.Range("A1")
LastCol = StartPoint.End(xlToRight).Column
DownCell = StartPoint.End(xlDown).Row
Set DataRange = Data_Sheet.Range(StartPoint, Cells(DownCell, LastCol))
NewRange = Data_Sheet.Name & "!" & DataRange.Address(ReferenceStyle:=xlR1C1)
Pivot_Sheet.PivotTables (PivotName)
ChangePivotCache.ActiveWorkbook PivotCaches.Create SourceType:=xlDatabase, SourceData:=NewRange Pivot_Sheet.PivotTables(PivotName).RefreshTable
Pivot_Sheet.Activate MsgBox "Your Pivot table is now updated" End Sub
谢谢
答案 0 :(得分:0)
这需要粘贴在包含将要更改的表的工作表上的 VBE 中。
按原样,当表格中的 ANY 单元格更改时,这将刷新工作簿上存在的每个数据透视表(以及数据透视表)。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.DisplayAlerts = False
Application.EnableEvents = False
ThisWorkbook.RefreshAll
Application.DisplayAlerts = True
Application.EnableEvents = True
Msgbox "All Pivots Refreshed"
End Sub