我正在使用以下代码:
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
但是当我运行代码时,出现了错误:“数据透视表字段名称无效。要创建数据透视表报告,您必须使用以标记列组织的列表数据。如果要更改数据透视表字段的名称,则必须键入新名称。”
这段代码之前一直能正常运行,但突然间出现了这个错误。
请问有人能帮我找出原因吗?
谢谢。