gpt4 book ai didi

excel - 创建数据透视表时过程调用或参数无效

转载 作者:行者123 更新时间:2023-12-04 21:17:07 27 4
gpt4 key购买 nike

尝试从一组数据创建数据透视表。通常它第一次运行良好,但如果您尝试第二次运行它,它会抛出“无效的过程调用或参数”,当您单击调试时,代码会突出显示以下问题。更改数据透视表的名称没有帮助。工作表 2 存在并填充有数据。数据摘要表也存在,但完全是空的

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet2!R1C1:R1064C4", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Data-Summary!R5C1", TableName:="PivotTable4", _
DefaultVersion:=xlPivotTableVersion

整个代码如下:

Sub Macro1()
'
' Macro1 Macro
'

'
Columns("A:D").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet2!R1C1:R1064C4", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Data-Summary!R5C1", TableName:="PivotTable15", _
DefaultVersion:=xlPivotTableVersion14
Sheets("Data-Summary").Select
Cells(5, 1).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Site")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Channel")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Cost"), "Count of Cost", xlCount
ActiveSheet.PivotTables("PivotTable3").PivotFields("Count of Cost"). _
Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Revenue"), "Count of Revenue", xlCount
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Count of Revenue")
.Caption = "Sum of Revenue"
.Function = xlSum
End With
ActiveWorkbook.ShowPivotTableFieldList = False
End Sub

最佳答案

更改表格目标以在工作表名称周围添加引号

TableDestination:="'Data-Summary'!R5C1"

还有一些代码是不必要的

Sub Macro1()
'
' Macro1 Macro
'

'
Dim PT As Excel.PivotTable
Set PT = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet2!R1C1:R1064C4", Version:=xlPivotTableVersion14).CreatePivotTable( _
TableDestination:="'Data-Summary'!R5C1", TableName:="PivotTable15", _
DefaultVersion:=xlPivotTableVersion14)
With PT
With .PivotFields("Site")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Channel")
.Orientation = xlColumnField
.Position = 1
End With
.AddDataField .PivotFields("Revenue"), "Sum of Revenue", xlSum
End With
End Sub

关于excel - 创建数据透视表时过程调用或参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17020446/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com