I've been extensively looking into creating a pivot table through vba, and am facing some complications I can't find a solution to. I am trying to create a pivot table in column 4 of the sheet "Pivot". When I try to run my code I get:
"Run time Error 1004: PivotTableWizard method of Worksheet class failed."
Can anyone help? I am still very new to vba. Here is my code, I keep getting error on the second line:
Sub PivotTable()
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Sheets("Information").UsedRange).CreatePivotTable TableDestination:="Pivot!R1C4", TableName:="PivotTable", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(1, 1)
With ActiveSheet.PivotTables("PivotTable").PivotFields("PN")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Commit")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable").AddDataField ActiveSheet.PivotTables("PivotTable").PivotFields("Qty"), "Sum", xlSum
End Sub
PivotTable. Comment out the second line, and step through. See what happens when you hit the third line. As long as your initial line creates the PT with the data you want, it should proceed. I am guessing you acquired this by usingRecord Macro, which is a great start. Try recording the Macro fresh without using the Wizard. It may help you understand how to create PT's strictly with VBA. - Busse