Solved

How to add another field to a pivot table using vba

Posted on 2015-02-05
4
66 Views
Last Modified: 2016-02-10
Hello,

I am working on pre-existing code.  I was asked to add an additional field to the pivot table called "Reason_Code".  I added it and it appears in the Pivot Table Field List but needs to be checked in order for it to appear in the pivot table itself

 Set PivotDataRange = rng
    Set PTCache = wb.PivotCaches.Create _
        (SourceType:=xlDatabase, _
        SourceData:=PivotDataRange, _
        Version:=xlPivotTableVersion12)
    Set sPT = PTCache.CreatePivotTable(TableDestination:=wsPiv.Cells(4, 1), TableName:="ScrapData", DefaultVersion:=xlPivotTableVersion12)
    
    With sPT.PivotFields( _
        "User_Transaction_Entered")
        .Orientation = xlPageField
        .Position = 1
    End With
    With sPT.PivotFields("Warehouse")
        .Orientation = xlPageField
        .Position = 1
    End With
    With sPT.PivotFields("Transaction_Date")
        .Orientation = xlColumnField
        .Position = 1
    End With
    With sPT.PivotFields("Item_Code")
        .Orientation = xlRowField
        .Position = 1
    End With
    With sPT.PivotFields("Item_Description")
        .Orientation = xlRowField
        .Position = 2
    End With

 With sPT.PivotFields("Reason_Code")
        .Orientation = xlRowField
        .Position = 3   
 End With

    sPT.AddDataField sPT.PivotFields("Qty"), "Quantity", xlSum
    sPT.AddDataField sPT.PivotFields("SumOfAmount"), "Amount", xlSum
    
    'Change number VBA.Format
    With sPT.PivotFields("Amount")
        .NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
    End With
    
    With sPT.PivotFields("Quantity")
        .NumberFormat = "#,##0.00_);[Red](#,##0.00)"
    End With
    
    'Sort largest to smallest
    sPT.PivotFields("Item_Code").AutoSort _
        xlDescending, "Amount", sPT.PivotColumnAxis.PivotLines(2), 1
    
    'Remove grand totals for rows
    sPT.RowGrand = False
        
    'Remove subtotals for each item
    sPT.PivotFields("Item_Code").Subtotals = Array _
        (False, False, False, False, False, False, False, False, False, False, False, False)
    '******************************************
       
    sPT.TableStyle2 = "PivotStyleLight16"
    
    With wsPiv.Cells.Font
        .Name = "Calibri"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    
    'Change display to Classic pivot table style
    With sPT
        .InGridDropZones = True
        .RowAxisLayout xlTabularRow
    End With
                    
    'Activate pivot table sheet & save workbook
    wsPiv.Activate
    wsPiv.Range("B7").Select
    wb.Save
    wb.Close

Open in new window

0
Comment
Question by:chtullu135
  • 3
4 Comments
 

Author Comment

by:chtullu135
ID: 40591339
When I copy the database, from which the excel pivot table is created (via automation), to my hard drive. And run the code from the local database, everything works fine.  The reason_code appears in the Pivot Table Fields List, and is checked and appears as a row in the Pivot Table.  It must be something in my installation.
0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40593271
can u portray here how do you want the Reason Code to be appeared? i am not sure if i understood your question.
0
 

Accepted Solution

by:
chtullu135 earned 0 total points
ID: 40593981
I was able to get it working.  I found that I had not saved the changes and so when I initially tested the code, it failed.  My mistake.
0
 

Author Closing Comment

by:chtullu135
ID: 40625309
It was my mistake
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now