excel - 错误代码:当相同的代码适用于不同的数据透视表时,“无法获取工作表类的数据透视表属性”
问题描述
我正在编写一个创建多个数据透视表的宏。除了 1 个表之外的所有表都运行良好,但我收到以下表的运行时错误代码:unable to get the pivottables property of the worksheet class
Dim PT As Excel.PivotTable
Set PT = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"GFCID!R1C1:R117200C129", Version:=xlPivotTableVersion14).CreatePivotTable( _
TableDestination:="'source'!R1C1", TableName:="Banking Book Pivot", _
DefaultVersion:=xlPivotTableVersion14)
With PT.PivotFields( _
"IRU")
.Orientation = xlRowField
.Position = 1
End With
' ActiveSheet.PivotTables("Banking Book Pivot").AddDataField ActiveSheet.PivotTables( _
' "Banking Book Pivot").PivotFields("GFCID"), "Count of GFCID", xlCount
' PT.AddDataField ActiveSheet.PivotTables( _
' "Banking Book Pivot").PivotFields("GFCID"), "Count of GFCID", xlCount
' With ActiveSheet.PivotTables("Banking Book Pivot").PivotTables("Banking Book Pivot").PivotFields("GFCID" & Chr(10) & "Count")
' .Orientation = xlDataField
' .Function = xlCount
' .Name = "Count of GFCID"
' End With
我已经尝试了被注释掉的三个代码中的每一个,它们都得到了相同的错误。前两个代码与宏中的其他数据透视表一起使用。该数据透视表的数据集比其他数据集大得多,但我认为这不会产生影响。
解决方案
不要使用ActiveSheet
. 使用PT
:
With PT
.AddDataField .PivotFields("GFCID"), "Count of GFCID", xlCount
End With
推荐阅读
- android - 带有数据绑定和 MVVM 的 Android ViewPager
- ffmpeg - 如何使用 ffmpeg 淡出视频的边缘?
- r - R创建一个数据框并将其用于ggplot
- javascript - 如果用户输入的单词存在于分配给变量的数组中,如何检查条件
- python - Python:避免每次运行代码时加载/读取数据
- javascript - Json Encode 在一台服务器上工作,在另一台服务器上给出未定义的错误
- cefsharp - EvaulateScriptAsPromiseAsync 和 async/await
- javascript - Ant Design 中带单选按钮的动态表单
- html - 使用 bookdown,我可以添加标签吗?
- matlab - 在 MATLAB 中计算两个变量的函数的 minmax