通过 VBA 更改枢轴源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18708126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:14:46  来源:igfitidea点击:

Change pivot source through VBA

vbaexcel-2007

提问by Sam

I get "Invalid procedure or call" when I use the code below in order to change the pivot source range in VBA. What is the right syntax for doing this? Thanks in advance.

当我使用下面的代码来更改 VBA 中的枢轴源范围时,我得到“无效的过程或调用”。这样做的正确语法是什么?提前致谢。

Dim DataArea As Variant
'...    
DataArea = "Raw_Data!R1C1:R" & Selection.Rows.Count & "C" & Selection.Columns.Count
Sheets("Pivot-view").Select
Range("A1").Select
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
   PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataArea, _
   Version:=xlPivotTableVersion14)

回答by Jaycal

If DataAreais a valid, named rangein your workbook, then the reference to it in your macro needs to be in quotes; therefore, update

如果DataArea是工作簿中有效的命名范围,则宏中对它的引用需要用引号引起来;因此,更新

SourceData:=DataArea,

SourceData:=DataArea,

to become

成为

SourceData:="DataArea",

SourceData:="DataArea",