I am trying to create a pivot table using vba. Following is the code that i tried:
Sub CreatePivot()
Dim objTable As PivotTable
Dim objField As PivotField
Dim ws As Worksheet
Set ws = Worksheets.Add
Sheets(ws.Name).Name = "Write-Off Pivot"
' Select the sheet and first cell of the table that contains the data.
ActiveWorkbook.Sheets("GEP Write-Offs Rawdata").Select
Range("A1").Select
Set objTable = ws.PivotTableWizard
' Specify row and column fields.
Set objField = objTable.PivotFields("MPG")
objField.Orientation = xlRowField
' Specify a data field with its summary
' function and format.
Set objField = objTable.PivotFields("'A_780610 - Inventory - Obsolescence")
objField.Orientation = xlDataField
objField.Function = xlSum
objField.NumberFormat = "$ #,##0"
End Sub
I am getting this error message. "Run-time error '104'" "method 'pivottables' of object '_worksheet' failed"
The problem seems to be in this line but not able to figure what's causing it.
Set objTable = ws.PivotTableWizard