我尝试使用 VBA 创建数据透视表,但遇到以下错误:
“运行时错误‘5’:
无效的过程调用或参数”
现在,我使用这个问题/答案来尝试解决我的问题 Runtime Error 5 Sometimes in VBA Creating PivotTable
我记录了我的宏,看看如果我正常制作数据透视表,它会生成什么代码。我将使用名为“报告”的现有工作表创建数据透视表。这是结果:(我删除了此代码片段中的大部分代码,因为很多是数据透视表属性 True/False 等)
Range("A3:CX1048576").Select
Range("F3816").Activate
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"OrderManagementData!R3C1:R1048576C102", Version:=8).CreatePivotTable _
TableDestination:="Stock & WIP Report!R1:R1048576", TableName:= _
"PivotTable2", DefaultVersion:=8
Sheets("Report").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable1")
.ColumnGrand = True
.RowAxisLayout xlCompactRow
End With
With ActiveSheet.PivotTables("PivotTable1").PivotCache
.RefreshOnFileOpen = False
.MissingItemsLimit = xlMissingItemsDefault
End With
ActiveSheet.PivotTables("PivotTable1").RepeatAllLabels xlRepeatLabels
现在,当我调试错误时,它会突出显示这部分代码:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"OrderManagementData!R3C1:R1048576C102", Version:=8).CreatePivotTable _
TableDestination:="Stock & WIP Report!R1:R1048576", TableName:= _
"PivotTable2", DefaultVersion:=8
我尝试将版本/默认版本更改为以下任何版本:xlPivotTableVersion15、xlPivotTableVersion14、xlPivotTableVersion12,但没有任何效果。
我的 Excel 版本不仅仅是 2010 或 2013。当我单击"file"->“帐户”->“关于 Excel”时,它会显示以下信息:Microsoft® Excel® for Microsoft 365 MSO(版本 2210 内部版本 16.0.15726.20070)64 位
知道如何创建数据透视表吗?花了一段时间在这上面,我被难住了
最佳答案
尝试以下操作:
TableDestination:="Stock & WIP Report!R1C1"
或
TableDestination:=ThisWorkbook.Worksheets("Stock & WIP Report").Range("A1")
请注意,Version:=8
和 DefaultVersion:=8
都是可选参数,如 PivotCaches.Create
中所述。和 PivotCache.CreatePivotTable
文档。
关于excel - 运行时错误 5 VBA 创建数据透视表 - 默认版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74446932/