Add pivot table fields to values using VBA

amitesh

New Member
Joined
Jul 24, 2008
Messages
2
Hi
I have managed to add Calculated fields to the pivot table as shown in the code below but I am not able to get these calculated fields to appear in the values list using vba. Can anybody please enlighten me on how I can add the calculated fields to the values list using vba


Portion of the code below: (pvtTable is a passed in pivot table into the method)

...

Code:
    Dim pc As PivotCache

    Set pc = wb.PivotCaches.Add(xlExternal)
    Dim pt As PivotTable

    pc.Connection = pvtTable.PivotCache.Connection
    pc.MaintainConnection = False
   
    
    
    Dim oldPivotDetails(1 To 24, 1 To 24) As String
    Dim count As Integer
    For count = LBound(oldPivotDetails) To UBound(oldPivotDetails)
        oldPivotDetails(count, 1) = pvtTable.CalculatedFields.Item(count).Name
        oldPivotDetails(count, 2) = pvtTable.CalculatedFields.Item(count).formula
    Next
    
    
    Set pt = pc.CreatePivotTable("", , False)
    pvtTable.CacheIndex = pc.index
    
    pvtTable.ManualUpdate = True
    
    
    For count = LBound(oldPivotDetails) To UBound(oldPivotDetails)
        pvtTable.CalculatedFields.Add Name:=oldPivotDetails(count, 1), formula:=oldPivotDetails(count, 2) 
    Next
    
    pvtTable.ManualUpdate = False
    
    
    pvtTable.SaveData = False
    pt.TableRange1.Worksheet.Delete
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,223,270
Messages
6,171,103
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top