VBA: Type Mismatch with PivotCaches

前端 未结 1 1042
小鲜肉
小鲜肉 2021-01-28 22:44

I am trying to insert a pivot table into a document. The following code throws a mismatch error, however, I am not sure where/why it is throwing it wit

相关标签:
1条回答
  • 2021-01-28 23:47

    You are creating a pivot table and try to fit it into a pivot cache object, that can't work! ;)

    Separate the last statement:

    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")
    
    0 讨论(0)
提交回复
热议问题