VBA - Add more than 1 pivot table data fields

sweeneytime

Board Regular
Joined
Aug 23, 2010
Messages
183
Hi,

I am automating a pivot table and I want more than 1 data fields. For the row fields it accepts an array:

HTML:
pt.AddFields RowFields:=Array("GPProject", "Department",)

But it won't accept that for the data fields, below is the code for a single field
HTML:
pt.AddDataField pt.PivotFields("FY13/14"), "Sum of Amount"

I have looked at this problem a few time with no solution. Would anyone know the answer or point me in the right direction?

I even have a pivot table book, but it some how doesn't cover this exact question :(

Cheers,
Sweeneytime

Full Code I'm using
HTML:
Sub ForecastPVBoard()

    Dim LR As Long
    Dim wb As Workbook
    Dim pt As PivotTable
    Dim pc As PivotCache
    Dim Data As Range
    
    'Find last row
    LR = Range("AD" & Rows.Count).End(xlUp).Row
 
    Sheets("Forecast Data").Select
    
    With Sheets("Forecast Data")
    
    'Data for pivot table
    Range("A1").Select
    Range(ActiveCell.Offset(0, 10), Selection.End(xlDown)).Select
    
    Set Data = Selection
   
    Range("A1").Select
    
    Data.Select
    

    Set wb = ActiveWorkbook
    
    Set pc = wb.PivotCaches.Add(SourceType:=xlDatabase, _
            SourceData:=Data)
    
    'Create PV
    
    Sheets("PV Report Board").Select
    
    Set pt = pc.CreatePivotTable _
        (Tabledestination:=wb.Worksheets("PV Report Board").Range("A1"), _
        TableName:="Data")
    
    'Add rows and column fields
    pt.AddFields RowFields:=Array("GPProject", "Department", "Organization_Name", "Person_Name", "Position", "Grade_Name", "GRade_Step", "Assignment_End_Date", "Time_Fraction")
    
    'Add Value Fields
    pt.AddDataField pt.PivotFields("FY13/14"), "Sum of Amount"
    
    Call NoSubtotals
    
    'PasteSpecial PV
    pt.TableRange2.Copy
    
    Range("A1").Select
    
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
   
 
    
    End With
       

    Application.ScreenUpdating = True
    Application.Calculation = xlAutomatic
    

End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,143,659
Messages
5,720,134
Members
422,267
Latest member
olund

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
Top