Hi,

I've written some vba code to create a pivot table (the input range may vary and has been accounted for)


' This creates Dynamic data range named "PvtData"

Sheets("data import month").Select
ActiveWorkbook.Names.Add Name:="PvtData", RefersToR1C1:= _
"=OFFSET('Data Import Month'!R1C1,0,0,COUNTA('Data Import Month'!C1),COUNTA('Data Import Month'!R1))"


Create the pivot table

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"PvtData", Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="Data Import Month!R3C4", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion14



Cells(3, 4).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Post_sector")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Policy_Cnt"), "Sum of Policy_Cnt", xlSum
End Sub


The highlighted red code is where i come across the error Run-time error 5: invalid procedure, call or argument

Any suggestions please