有人可以让我知道我在下面的代码中做错了什么。我看了很多线程,但仍然无法解决这个问题。它不断抛出消息Unable to get the PivotFields property of the PivotTable Class
Sub Macro1()
Dim sht As Worksheet
Dim pc As PivotCache
Dim pt As PivotTable
Dim i As Long
Application.DisplayAlerts = False
Application.ScreenUpdating = False
ActiveSheet.Name = "Data"
Set sht = ActiveWorkbook.Sheets("Data")
Set myrange = sht.Range(sht.Cells(2, 1), sht.Cells(ActiveSheet.UsedRange.Rows.Count - 1, Columns.Count).End(xlToLeft))
Worksheets.Add
Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:= _
xlDatabase, SourceData:=myrange)
Set pt = pc.CreatePivotTable(TableDestination:=ActiveSheet.Range("B3"), TableName:="PivotTable1")
With pt
With .PivotFields("Provider Name (for charge)")
.Orientation = xlRowField
.Position = 1
End With
.CalculatedFields.Add "Net Charges", "=Charge-ChgCorr"
.CalculatedFields.Add "Net Payments", "=Pymnt,UP-PayCor,UPC"
.CalculatedFields.Add "Total Adjustments", "=Contract WO, WOC+Other WO,WOC"
With pt
With .PivotFields("Net Charges")
.Orientation = xlDataField
.Position = 1
End With
With .PivotFields("Net Payments")
.Orientation = xlDataField
.Position = 2
End With
With .PivotFields("Total Adjustments")
.Orientation = xlDataField
.Position = 3
End With
End With
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
很难说没有看到Excel工作簿,但您应该仔细检查您指定的PivotFields的名称是否正确。
您可能希望尝试单步执行代码,以确切了解它所在的行。