我试图创建一个宏来创建一个基于数据一致位置的数据透视表。然而,我正在努力克服这个最初的挑战。我一直在遵循创建Pivot宏的指南,但是错误438始终以宏结尾。这段代码看起来应该有效,但我不知道为什么不行。
Sub StockAdjustmentPivot()
Dim myFirstRow As Long
Dim myLastRow As Long
Dim myFirstColumn As Long
Dim myLastColumn As Long
Set sht = ActiveSheet
Dim mySourceData As String
Dim myDestinationRange As String
Dim myPivotTable As PivotTable
myDestinationRange = sht.Range("T5").Address(ReferenceStyle:=xlR1C1)
myFirstRow = 1
myLastRow = sht.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
myFirstColumn = 1
myLastColumn = 11
With sht.Cells
mySourceData = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn)).Address(ReferenceStyle:=xlR1C1)
End With
Set myPivotTable = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="STOCK ADJUSTMENTS REPORT!" & mySourceData).CreatePivotTables(TableDestination:="STOCK ADJUSTMENTS REPORT!" & myDestinationRange, DefaultVersion:="PivotTable1")
End Sub显示错误的行如下:
Set myPivotTable = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="STOCK ADJUSTMENTS REPORT!" & mySourceData).CreatePivotTables(TableDestination:="STOCK ADJUSTMENTS REPORT!" & myDestinationRange, DefaultVersion:="PivotTable1")在继续进行枢轴表的详细内容之前,我正在尝试修复这个问题。
谢谢。
编辑
我已经将代码拆分为更易于管理的大小,如指南所示:
Dim cache As PivotCache
Set cache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="STOCK ADJUSTMENTS REPORT!" & mySourceData)
Set myPivotTable = cache.CreatePivotTable(TableDestination = "STOCK ADJUSTMENTS REPORT!" & myDestinationRange, TableName:="PivotTable1")但是,我认为没有定义缓存变量。运行此操作会出现以下错误:运行时错误5:无效的过程调用或参数。
发布于 2018-01-08 16:27:23
泰瑞:你有一个错误。将CreatePivotTables更改为CreatePivotTable。
你有一条指令做了太多的事情:
设置ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase,= myPivotTable SourceData:=“股票调整报告!”& mySourceData).CreatePivotTables(TableDestination:="STOCK调整报告!& myDestinationRange,DefaultVersion:="PivotTable1")
这意味着访问ActiveWorkbook的pivot集合,使用它创建一个新的pivot缓存,同时使用它创建一个pivot表。
Excel -- PivotCache没有CreatePivotTables成员-因此,对象不支持此属性或方法。
将其拆分,并使用正确声明的变量。
Dim cache As PivotCache
Set cache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="STOCK ADJUSTMENTS REPORT!" & mySourceData)如果这样做有效,那么您可以使用它来创建枢轴表--如果它不能工作,至少现在您知道哪一部分失败了(您越多地将内容插入到单个指令中,该指令就越难调试):
Dim pivot As PivotTable
Set pivot = cache.CreatePivotTable(TableDestination:="STOCK ADJUSTMENTS REPORT!" & myDestinationRange, DefaultVersion:="PivotTable1")请注意,当您在IntelliSense之后键入.点时,VBE提供了一个就地下拉(“cache”):聆听编辑器告诉您的内容,这样就可以避免这种错误。
https://stackoverflow.com/questions/48154020
复制相似问题