我有一个在另一个工作表中的计划,而不是工作簿,并且已根据该数据更新了数据透视表。
问题是......当我尝试使用下面的宏时,我得到了一个
运行时错误'9'下标超出范围。
P.S。 >>> “Schedule 02-26”每天根据日期变化。
Sub Change_Pivot_Source()
Dim pt As PivotTable
For Each pt In ActiveWorkbook.Worksheets("Schedule 02-26").PivotTables
pt.ChangePivotCache ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:="Schedule!$A$1:$AK$1200")
Next pt
End Sub
轻松修复?
更新:“无法在休息模式下执行代码”???
我将宏代码放在“本工作簿”中
答案 0 :(得分:0)
'This might be helpful, starting out on your pivot table sheet
Sub PivotUpdate()
Dim PivotSheet as string
Dim Data As Range
Dim lrow As Long
Dim PivotCount As Long
Dim i As Long
PivotSheet = activesheet.name
sheets("schedule").select
'get the last row where your data is
lrow = sheets("schedule").Range("A1").End(xlDown).Row
Set Data = sheets("schedule").Range("A1:AK" & lrow & "")
Sheets(PivotSheet).Select
PivotCount = Sheets(PivotSheet).PivotTables.Count
For i = 1 To PivotCount
Sheets(PivotSheet).PivotTables(i).ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Data, _
Version:=xlPivotTableVersion12)
'version12 is excel 2007
Next i
End Sub