VBA:types与PivotCaches不匹配
我试图插入一个pivot table
到文档中。 下面的代码抛出了一个mismatch
错误,但是,我不知道在哪里/为什么抛出它在一行( Set pCache
…行。抛出错误时,空的pivot table
仍然被创build。
我哪里错了?
pivot_lastRow = mainSheet.Cells(mainSheet.Rows.count, "A").End(xlUp).Row 'may need to change "A" pivot_lastCol = mainSheet.Cells(1, mainSheet.Columns.count).End(xlToLeft).Column Set pivotRange = mainSheet.Range(mainSheet.Cells(1, 1), mainSheet.Cells(pivot_lastRow, pivot_lastCol)) Set pCache = wbk.PivotCaches.Create(SourceType:=xlDatabase, _ SourceData:=pivotRange).CreatePivotTable(TableDestination:=pivotSheet.Cells(2, 2), _ TableName:="Test")
您正在创build一个数据透视表,并尝试将其放入一个透视caching对象,这是无法正常工作! ;)
分开最后的声明:
Dim pCache As PivotCache Dim pTable As PivotTable pivot_lastRow = mainSheet.Cells(mainSheet.Rows.count, "A").End(xlUp).Row 'may need to change "A" pivot_lastCol = mainSheet.Cells(1, mainSheet.Columns.count).End(xlToLeft).Column Set pivotRange = mainSheet.Range(mainSheet.Cells(1, 1), mainSheet.Cells(pivot_lastRow, pivot_lastCol)) Set pCache = wbk.PivotCaches.Create(SourceType:=xlDatabase, _ SourceData:=pivotRange) Set pTable = pCache.CreatePivotTable(TableDestination:=pivotSheet.Cells(2, 2), _ TableName:="Test")