简体   繁体   English

自动更新数据透视表数据源

[英]Automatically update PivotTable data source

Using the ole' search that stackoverflow provides, i found the following code 使用stackoverflow提供的ole'搜索,我找到了以下代码

Sub Change_Pivot_Source()

Dim pt As PivotTable

For Each pt In ActiveWorkbook.Worksheets("Funnel Data Pivots").PivotTables
    pt.ChangePivotCache ActiveWorkbook.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:="Lead Data (Q1-Q2)")
Next pt

End Sub

Where "Funnel data Pivots" is my woorksheet that contains all my pivot charts i am trying to update, and "Lead Data (Q1-Q2)" is the data source of all my pivots....Each week the data source is extended (we input lines of data at the bottom, keeping the rest of the data as it is)....yet this code fails to render, saying "Reference is not valid" (on the Lead Data (Q1-Q2) line...I assumed I typod, so i changed the name in the worksheet (and in the VBA) to a much simpler text...yet it still failed. “ Funnel data Pivots”是我的woorksheet,其中包含我要更新的所有数据透视表,“ Lead Data(Q1-Q2)”是我所有数据透视的数据源。...每周扩展数据源(我们在底部输入数据行,其余数据保持原样)。...但是此代码未能呈现,并说“参考无效”(在潜在客户数据(Q1-Q2)行上)。 ..我以为是我输入的,所以我将工作表(和VBA)中的名称更改为更简单的文本...但是仍然失败。

So 所以

A) Is this the right code I want to use to extend the data source of my pivot tables? A)这是我想用来扩展数据透视表的数据源的正确代码吗?

and

B) if it is, how do i debug it, if it's not the right code, what is? B)如果是,我应该如何调试它,如果它不是正确的代码,那是什么?

This was my original VBA function, which updated pivot tables, but not the data source 这是我最初的VBA函数,它更新了数据透视表,但没有更新数据源

Sub AllWorksheetPivots1()

Dim pt As PivotTable

For Each pt In ActiveSheet.PivotTables

    pt.RefreshTable

Next pt


End Sub

Thanks for your time! 谢谢你的时间!

The SourceData argument requires a Range object per help, but it appears you are supplying it with a sheet name. 每个帮助的SourceData参数都需要一个Range对象,但是您似乎要为其提供一个工作表名称。 My experience is that it actually wants a string that represents a range. 我的经验是,它实际上需要代表范围的字符串。 So you should be using something like 所以你应该使用类似

SourceData:="Lead Data (Q1-Q2)!A1:D100"

But what would be even better is if you made your data a table and based your pivottable on that. 但是,如果将数据制成表并基于数据透视表,那就更好了。 Then your pivot table will automatically expand as you add new rows. 然后,当您添加新行时,数据透视表将自动展开。

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

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