Wednesday 29 August 2012

VB.NET - Excel - how to fix error "Unable to set the Function property of the PivotField class"

VB.NET - Excel - how to fix error "Unable to set the Function property of the PivotField class"   


Replace
Dim xlRange2 As Excel.Range = CType(osheet, Excel.Worksheet).Range("A2")
Dim ptCache As Excel.PivotCache = obook.PivotCaches.Add(XlPivotTableSourceType.xlDatabase, xlRange)
Dim ptTable As Excel.PivotTable = osheet.PivotTables.Add(PivotCache:=ptCache, TableDestination:=xlRange2, TableName:="Summary")
With ptTable
.ManualUpdate = True
.PivotFields("Date").Orientation = Excel.XlPivotFieldOrientation.xlColumnField
.PivotFields("NetSales").Orientation = Excel.XlPivotFieldOrientation.xlDataField
.PivotFields("NetSales").Function = Excel.XlConsolidationFunction.xlSum
.ManualUpdate = False
End With

With
Dim xlRange2 As Excel.Range = CType(osheet, Excel.Worksheet).Range("A2")
Dim ptCache As Excel.PivotCache = obook.PivotCaches.Add(XlPivotTableSourceType.xlDatabase, xlRange)
Dim ptTable As Excel.PivotTable = osheet.PivotTables.Add(PivotCache:=ptCache, TableDestination:=xlRange2, TableName:="Summary")
Dim ptField As Excel.PivotField = ptTable.PivotFields("NetSales")
With ptField
.Orientation = Excel.XlPivotFieldOrientation.xlDataField
.Function = Excel.XlConsolidationFunction.xlSum
End With
With ptTable
.ManualUpdate = True
.PivotFields("Date").Orientation = Excel.XlPivotFieldOrientation.xlColumnField
.ManualUpdate = False
End With

No comments:

Post a Comment