vba 创建数据透视表的问题,这是 excel 版本问题吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15301158/
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
Issue with vba creating a pivot table, is this an excel version issue?
提问by William Fuller
Quick one, got the following code which goes and creates a pivot table based on a data collection "Data". I run this on the PC that it was created on which runs Excel 2013 without an issue however when I run it on Excel 2007 or Excel 2010 it throws up an Invalid Procedure Call or Arguement error on the line after Range("A1").Select
很快,得到以下代码,该代码基于数据集合“Data”创建数据透视表。我在创建它的 PC 上运行它,该 PC 运行 Excel 2013 没有问题,但是当我在 Excel 2007 或 Excel 2010 上运行它时,它会在 Range("A1") 之后的行上抛出一个无效的过程调用或参数错误。选择
Is this an excel version error because written in Excel 2013 but used on 2007 or 2010?
这是一个 excel 版本错误,因为用 Excel 2013 编写但在 2007 或 2010 上使用?
Sheets("Pivot").Select
Range("A1").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data", Version:=xlPivotTableVersion15).CreatePivotTable TableDestination:= _
"Pivot!R1C1", TableName:="PivotTable4", DefaultVersion:= _
xlPivotTableVersion15
Sheets("Pivot").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable4").PivotFields("Date Only")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable4").PivotFields("Species")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable4").AddDataField ActiveSheet.PivotTables( _
"PivotTable4").PivotFields("Number"), "Sum of Number", xlSum
If it is a version issue which I think it might be as people have used DefaultVersion:=xlPivotTableVersion14 and 12 does anyone know which version would work on Excel 2007 or Excel 2010? I am assuming if I input correct version for 2007 it would be backwards compatible with 2010 and 2013 etc...?
如果这是一个版本问题,我认为这可能是因为人们使用过 DefaultVersion:=xlPivotTableVersion14 和 12 有谁知道哪个版本适用于 Excel 2007 或 Excel 2010?我假设如果我输入正确的 2007 版本,它将向后兼容 2010 和 2013 等......?
Cheers in advance Will
提前干杯威尔
回答by Doug Glancy
I think it's an issue with Version:=xlPivotTableVersion15
, which is specific to Excel 2013. Here's the help from Excel 2010:
我认为Version:=xlPivotTableVersion15
这是 Excel 2013 特有的问题。这是 Excel 2010 的帮助:
It is an optional argument and, in Excel 2010, the pivot table was created correctly with and without it.
它是一个可选参数,在 Excel 2010 中,无论是否使用它,都可以正确创建数据透视表。
回答by jason
I imagine it would be backwards compatible but you could always check the version and build your code accordingly. I believe
我想它会向后兼容,但你总是可以检查版本并相应地构建你的代码。我相信
application.version
will get you the current version in excel.
将为您提供 excel 中的当前版本。