VBA:类型与PivotCaches不匹配

河流31334

我正在尝试将apivot table插入文档中。以下代码将引发mismatch错误,但是,我不确定它在行(Set pCache...行)中的位置/原因。在引发错误时,pivot table仍会创建空白

我要去哪里错了?

    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")
R3uK

您正在创建数据透视表,并尝试将其放入数据透视表缓存对象中,这是行不通的!;)

分开最后一条语句:

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")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章