I'm using the following code:
Sub MakeAPivotTable()
Dim pt As PivotTable
Dim cacheOfpt1 As PivotCache 'This is the Source Data
Dim pf As PivotField
Dim pi As PivotItem
Dim LastCol As Long
Dim LastRow As Long
Dim PRange As Range
LastCol = Sheets("Sheet2").Range("A1").SpecialCells(xlCellTypeLastCell).Column
LastRow = Sheets("Sheet2").Range("A1").SpecialCells(xlCellTypeLastCell).Row
Set PRange1 = Sheets("Sheet2").Cells(1, 1).Resize(LastRow, LastCol)
On Error GoTo err_add_Y_next_row
Sheets("Sheet3").Select
ActiveSheet.PivotTables("PivotTable1").TableRange2.Clear 'Delete any Pivot Table
'set the cache of PT
Sheets("Sheet2").Select
Set cacheOfpt1 = ActiveWorkbook.PivotCaches.Create(xlDatabase, PRange1)
'create the pt
Sheets("Sheet3").Select
Set pt = ActiveSheet.PivotTables.Add(cacheOfpt1, Range("a1"), "PivotTable1")
'put the Fields in
With pt
'add the Fields
.PivotFields("CAMPAIGN").Orientation = xlRowField
'.PivotFields("TAG1").Orientation = xlRowField
'.PivotFields("TAG2").Orientation = xlRowField
'.PivotFields("TAG3").Orientation = xlRowField
.PivotFields("CIRN").Orientation = xlColumnField
.PivotFields("RUN").Orientation = xlPageField 'Column Filter
.PivotFields("TVSN").Orientation = xlDataField 'Value Field
'set the number format
.DataBodyRange.NumberFormat = "#,##0.00"
'go to classic View
.RowAxisLayout xlTabularRow
End With
err_add_Y_next_row:
MsgBox Err.Number & Err.Description, _
vbExclamation, "VBAtools.pl"
End Sub
But when I'm running the code, I'm getting the error "The PivotTable field name is not valid. To create a pivot table report you must use data that is organized as a list with labeled columns, If you are changing the name of a PivotTable field, you must type a new name for the field."
This code was running fine. But suddenly I started getting this error.
Can anybody please help me find the reason.
Thank you.