VBA:类型与数据透视缓存不匹配

VBA:类型与数据透视缓存不匹配,vba,excel,Vba,Excel,我正在尝试将透视表插入文档中。下面的代码抛出了错配错误,但是,我不确定它在哪里(为什么/为什么要将它扔进行内)(设置PCACHE/SCOR>……行。当错误被抛出时,空白枢轴表< /代码>仍然被创建。 我哪里做错了 pivot_lastRow = mainSheet.Cells(mainSheet.Rows.count, "A").End(xlUp).Row 'may need to change "A" pivot_lastCol = mainSheet.Cells(1, mai

我正在尝试将透视表插入文档中。下面的代码抛出了<代码>错配错误,但是,我不确定它在哪里(为什么/为什么要将它扔进行内)(<代码>设置PCACHE/SCOR>……行。当错误被抛出时,空白<代码>枢轴表< /代码>仍然被创建。

我哪里做错了

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

您正在创建数据透视表,并尝试将其放入数据透视缓存对象中,但该对象无法工作!;)

将最后一句话分开:

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

您正在创建数据透视表,并尝试将其放入数据透视缓存对象中,但该对象无法工作!;)

将最后一句话分开:

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

虽然没有显示
pCache
的声明,但这应该是解决方案。(声明
pcache和pTable
将是一个很好的补充)就是这样。谢谢你的帮助。@Rivers31334:很高兴我能帮忙!;)@A.s.h:我编辑了我的答案,以包含声明!虽然没有显示
pCache
的声明,但这应该是解决方案。(声明
pcache和pTable
将是一个很好的补充)就是这样。谢谢你的帮助。@Rivers31334:很高兴我能帮忙!;)@A.s.h:我编辑了我的答案,以包含声明!