2017-02-07 21 views
0

文書にpivot tableを挿入しようとしています。次のコードは、しかし、私はエラーを空白pivot tableがまだ作成され、スローされますがどこ/なぜそれが。(ライン内Set pCache ...ラインを、それを投げているかわからない、mismatchエラーがスローされます。VBA:PivotCachesとタイプの不一致

どこ?!私は間違っています

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

答えて

3

あなたは、ピボットテーブルを作成し、ピボットキャッシュオブジェクトにそれに合うようにしようとしている、それが動作することはできません。)

別々の最後の文:

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

'pCache'の宣言は表示されませんでしたが、これが解決策になるはずです。 (Declarations 'pcacheとpTable'でも良いでしょう) –

+0

これはそれです。お手伝いありがとう。 – Rivers31334

+0

@ Rivers31334:うれしい! ;) – R3uK