1
新しいタブでピボットテーブルを作成するVBAを作成しようとしています。コードを実行すると、ピボットテーブルキャッシュを作成するときにタイプの不一致エラーが発生します。VBAピボットテーブル
コードは、任意の助けがうまくいけばそれで新鮮な表情が私は
Sub makeAPivotTable()
Dim sSheet, sSheet2 As Worksheet 'sSheet is where the data is, sSheet2 is where the pivot will be built
Dim cCache As PivotCache
Dim ptTable As PivotTable
Dim rRange As Range
Dim rLastRow, cLastColumn As Long
' Insert the new sheet for the pivot table to reside
Application.DisplayAlerts = False
Sheets.Add Before:=ActiveSheet
ActiveSheet.name = "PivotTable"
Application.DisplayAlerts = True
Set sSheet2 = Worksheets("PivotTable")
Set sSheet = Worksheets("Interactions db")
' define the range (the data that you want to put into the pivot table
rLastRow = sSheet.Cells(Rows.Count, 1).End(xlUp).Row
cLastColumn = sSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set rRange = sSheet.Cells(1, 1).Resize(rLastRow, cLastColumn)
' create the cache for the pivot table
Set cCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=rRange). _
CreatePivotTable(TableDestination:=sSheet2.Cells(2, 2), _
TableName:="SalesPivotTable")
' insert the blank table
Set ptTable = cCache.CreatePivotTable _
(TableDestination:=sSheet2.Cells(1, 1), TableName:="SalesPivotTable")
'Insert Row Fields
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("customer")
.Orientation = xlRowField
.Position = 1
End With
'Insert Column Fields
'With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Interaction Type")
'.Orientation = xlColumnField
'.Position = 1
'End With
'Insert Data Field
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("interactionType")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##0"
.name = "Person "
End With
' do some formatting
End Sub
私は間違っていないよ場合、それはこのようにする必要があります - セットptTable = sSheet2.PivotTables.Add(PivotCache:= ccacheの、TableDestination:= sSheet2.Range( "A1")、テーブル名:= "SalesPivotTable") – sktneer
もそうでなければピボット・キャッシュを削除するために、最初にsSheet2を削除することが重要ですeコードが実行されると、同じ名前の2枚のシートを持つことができず、新しいピボット・キャッシュも作成され、不要に番号が増えるため、エラーが発生します。ワークブック内のピボット・キャッシュのサイズ、したがってファイルのサイズ。 – sktneer
@synneerあなたが正しいですが、エラー処理の可能性はたくさんありますが、まずPOがコード内の重大なエラーを解決する必要があると思います。しかし、あなたの推薦のために私はそれを(別の方法で)追加しました –