• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Add Dynamic calculated item(s) to a pivot table

rbnaik

New Member
have an excel sheet where data gets added every two minutes alongwith timeline. I have attached sample file of data and pivot table.

And here is the pivot table generated out of it
F&O*
Sum of LTPLupdate
Name10:35:0010:52:0011:15:0011:25:0012:13:00
ACC1,883.951,864.951,863.401,862.001,857.00
Adani Enterprises922.80
Ambuja Cements295.60293.55
Axis Bank739.50737.20
Adani Ports & Special Economic Zone753.60

I am trying to refresh pivot table which gives me price difference for a stock between last and second last time when data is updated (Both these values will be dynamic), so that I can track progress and trade according to the direction of market.
However, I am unable to add this dynamic field to my pivot table. Following is the code I am trying to modify to suit my need.

' '*************** Perfectly working example if I calculate manually
' ActiveSheet.PivotTables("PivotTable2").PivotFields("Lupdate").CalculatedItems. _
' Add "Diff", "='12:13:00' -'11:25:00'", True
'
' ActiveSheet.PivotTables("PivotTable2").PivotFields("Lupdate").CalculatedItems. _
' Add "Diff %", "=IFERROR(('12:13:00' -'11:25:00' )/'12:13:00',0)", True
' '******************* Upto this line

Complete subroutine is as follows:

>>> use code - tags <<<
Code:
Sub Macro1()
'
' Macro1 Macro
'

' Add calculated items (not fields)
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Gainer Pivot")

'Store last and second last updated time for calculation
last = Application.WorksheetFunction.Large(ws.Range("4:4"), 1)
secondlast = Application.WorksheetFunction.Large(ws.Range("4:4"), 2)
ActiveSheet.PivotTables("PivotTable2").PivotCache.Refresh
'MsgBox last & " " & secondlast

ActiveSheet.PivotTables("PivotTable2").PivotFields("Lupdate").CalculatedItems. _
Add "Diff", "='" & last & "' -'" & secondlast & "'", True

End Sub
Any help will be much appreciated. I am using office 2019.
 

Attachments

  • Dynamic file.xlsm
    127.1 KB · Views: 0
Last edited by a moderator:
Back
Top