Consulting

Results 1 to 5 of 5

Thread: Vba Pivot table

  1. #1

    Vba Pivot table

    I am using some code from a VBA book by John Wakenbach to create a pivot table. It works great but I need a calculated result "BlendVol" but not "Blends". Is there a way to hide the Blends field from the pivot table?

    ' Create the Pivot Table from the Cache
    Set PT = ActiveSheet.PivotTables.Add(PivotCache:=PTcache, TableDestination:=Range("A1"), TableName:="PIMSBlends")
    With PT
    ' Add fields
    .PivotFields("CaseName").Orientation = xlPageField
    .PivotFields("PeriodDescription").Orientation = xlPageField
    '.PivotFields("ReportGroup").Orientation = xlRowField
    .PivotFields("BlendTag").Orientation = xlRowField
    .PivotFields("PeriodLength").Orientation = xlColumnField
    .PivotFields("PeriodID").Orientation = xlColumnField
    .PivotFields("CaseID").Orientation = xlColumnField
    .PivotFields("VolActivity").Orientation = xlDataField
    '.PivotFields("Actual").Orientation = xlDataField
    '.DataPivotField.Orientation = xlRowField
    ' Add a calculated field to compute variance
    .CalculatedFields.Add "BlendVol", "=PeriodLength*VolActivity"
    .PivotFields("BlendVol").Orientation = xlDataField
    ' Specify a number format
    .DataBodyRange.NumberFormat = "0.00"
    ' Apply a style
    .TableStyle2 = "PivotStyleMedium15"""
    ' Hide Field Headers
    .DisplayFieldCaptions = False
    ' Change the captions
    .PivotFields("Sum of VolActivity").Caption = "Blends"
    .PivotFields("Sum of BlendVol").Caption = "Total Blend"
    
    
    End With

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    If you don't want that field, just don't add it to the table:

     ' Create the Pivot Table from the CacheSet PT = ActiveSheet.PivotTables.Add(PivotCache:=PTcache, TableDestination:=Range("A1"), TableName:="PIMSBlends")
    With PT
         ' Add fields
        .PivotFields("CaseName").Orientation = xlPageField
        .PivotFields("PeriodDescription").Orientation = xlPageField
         '.PivotFields("ReportGroup").Orientation = xlRowField
        .PivotFields("BlendTag").Orientation = xlRowField
        .PivotFields("PeriodLength").Orientation = xlColumnField
        .PivotFields("PeriodID").Orientation = xlColumnField
        .PivotFields("CaseID").Orientation = xlColumnField
         '.PivotFields("Actual").Orientation = xlDataField
         '.DataPivotField.Orientation = xlRowField
         ' Add a calculated field to compute variance
        .CalculatedFields.Add "BlendVol", "=PeriodLength*VolActivity"
        .PivotFields("BlendVol").Orientation = xlDataField
         ' Specify a number format
        .DataBodyRange.NumberFormat = "0.00"
         ' Apply a style
        .TableStyle2 = "PivotStyleMedium15"""
         ' Hide Field Headers
        .DisplayFieldCaptions = False
         ' Change the captions
        .PivotFields("Sum of BlendVol").Caption = "Total Blend"
         
         
    End With
    Be as you wish to seem

  3. #3
    But that column is volactivity, if I leave it out I'm not able to calculate BlendVol:

    .CalculatedFields.Add "BlendVol", "=PeriodLength*VolActivity"

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    I'm not sure I understand but AFAIK you can hide a field but still use in in a calculated field

    ActiveSheet.PivotTables("PivotTable1").PivotFields("CCC").Orientation = xlHidden
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Quote Originally Posted by AleemAM123 View Post
    But that column is volactivity, if I leave it out I'm not able to calculate BlendVol:
    What makes you say that? A field does not have to be part of a pivot table to be used in a calculated field.
    Be as you wish to seem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •