I have the following code which adds some fields to a Pivottable:
Set ptView = PivotTable.ActiveView
' The following lines of code add fields to the row area and data areas of the PivotTable list. ptView.RowAxis.InsertFieldSet ptView.FieldSets("CATEGORY")
ptView.DataAxis.InsertTotal ptView.AddTotal("Sales", ptView.FieldSets("Sales").Fields(0), PivotTable.Constants.plFunctionSum)
ptView.DataAxis.InsertTotal ptView.AddTotal("Cost", ptView.FieldSets("Cost").Fields(0), PivotTable.Constants.plFunctionSum)
ptView.DataAxis.InsertTotal ptView.AddTotal("Margin", ptView.FieldSets("Margin").Fields(0), PivotTable.Constants.plFunctionSum)
ptView.DataAxis.InsertTotal ptView.AddCalculatedTotal("Marginp", "Margin%", "([Measures].[Margin]/[Measures].[Sales])")
The last line works fine if [Measures].[Sales] <> 0, but I cannot figure out the correct syntax for the IF statement:
IF [Measures].[Sales] <> 0 THEN [Measures].[Margin]/[Measures].[Sales] ELSE 0
Set ptView = PivotTable.ActiveView
' The following lines of code add fields to the row area and data areas of the PivotTable list. ptView.RowAxis.InsertFieldSet ptView.FieldSets("CATEGORY")
ptView.DataAxis.InsertTotal ptView.AddTotal("Sales", ptView.FieldSets("Sales").Fields(0), PivotTable.Constants.plFunctionSum)
ptView.DataAxis.InsertTotal ptView.AddTotal("Cost", ptView.FieldSets("Cost").Fields(0), PivotTable.Constants.plFunctionSum)
ptView.DataAxis.InsertTotal ptView.AddTotal("Margin", ptView.FieldSets("Margin").Fields(0), PivotTable.Constants.plFunctionSum)
ptView.DataAxis.InsertTotal ptView.AddCalculatedTotal("Marginp", "Margin%", "([Measures].[Margin]/[Measures].[Sales])")
The last line works fine if [Measures].[Sales] <> 0, but I cannot figure out the correct syntax for the IF statement:
IF [Measures].[Sales] <> 0 THEN [Measures].[Margin]/[Measures].[Sales] ELSE 0