ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Pivot table calculated field (https://www.excelbanter.com/excel-discussion-misc-queries/215752-pivot-table-calculated-field.html)

nc

Pivot table calculated field
 
I have two macros below, to one to create and the other to delete calculated
field.

When I run create, the macro completes successfully. When I run the latter
I get an error message "Method 'Delete' of object 'PivotField' failed. Can
you please help me amend the 'DeleteCalcFields_Click() macro.



Private Sub DeleteCalcFields_Click()

For Each fld In _
Worksheets("Create calc
fields").PivotTables("PivotTable1").CalculatedFiel ds
fld.Delete
Next

End Sub

Private Sub CreateCalcFields_Click()

Dim rngItems As Range
Dim ws As Worksheet
Dim pt As PivotTable
Dim c As Range
Dim pf As PivotField


Set ws = Worksheets("Create calc fields")
Set pt = ws.PivotTables("PivotTable1")
Set rngItems = ws.Range("AddItems")

For Each c In rngItems
On Error Resume Next
Set pf = ws.PivotTables("PivotTable1").CalculatedFields(c.V alue)
On Error GoTo 0
If Not pf Is Nothing Then
ws.PivotTables("PivotTable1").CalculatedFields(c.V alue) _
.StandardFormula = c.Offset(0, 1).Value
Else
ws.PivotTables("PivotTable1").CalculatedFields.Add _
c.Value, c.Offset(0, 1).Value, True
' pt.PivotFields(c.Value).Orientation = xlDataField
End If
Next c

End Sub


Herbert Seidenberg

Pivot table calculated field
 
Add:
Dim fld As PivotField

nc

Pivot table calculated field
 


"Herbert Seidenberg" wrote:

Add:
Dim fld As PivotField


Herbert. Thanks for your suggestion, but unfortunately it did not solve the
problem.

Herbert Seidenberg

Pivot table calculated field
 
Here is my full example:
http://www.mediafire.com/file/jymwmum5nyg/01_02_09.xlsm



All times are GMT +1. The time now is 04:05 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com