我已经开发了数据透视表,需要添加一个计算字段。我想在计算中使用“引用”字段,而不是使用“命名”(行)字段。使用“命名”字段,该公式可以正常工作。但在使用“参考”时则不会。下面是代码的一部分,哪里出了问题。感谢您的指导和帮助。
'/-------------
'/ Insert Data Field
'/-------------
With pTable
With .PivotFields(wksDSheet.Cells(1, 5).Value)
.Orientation = xlDataField
.Position = 1
.Function = xlSum
'NumberFormat = "#,##0.00"
.Caption = "Actual Spent"
End With
s1 = pTable.PivotFields(wksDSheet.Cells(1, 5).Value)
With .PivotFields(wksDSheet.Cells(1, 6).Value)
.Orientation = xlDataField
.Position = 2
.Function = xlSum
'.NumberFormat = "#,##0.00"
.Caption = "YmBudget To date"
End With
With .PivotFields(wksDSheet.Cells(1, 7).Value)
.Orientation = xlDataField
.Position = 3
.Function = xlSum
'.NumberFormat = "#,##0.00"
.Caption = "YrBudget To Year"
s2 = pTable.PivotFields(wksDSheet.Cells(1, 7).Value)
End With
结尾为
'/-------------------
'/ Insert Calculated Fields
'/-------------------
'/ The Name and Formula
''' This works''''>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
' pTable.CalculatedFields.Add Name:="Available", _
Formula:="=YrBudget - Actual"
''' This does NOT work ''''>>>>>>>>>>>>>>>>>>>>>>>>
pTable.CalculatedFields.Add Name:="Available", _
Formula:="=s2 - s1"
With pTable.PivotFields("Available")
.Orientation = xlDataField
.Function = xlSum
.Position = 4
'.NumberFormat = "0.00"
.Caption = "Net Available"
End With