Consulting

Results 1 to 8 of 8

Thread: Toggle SUM/AVG on Pivot Table

  1. #1
    VBAX Newbie
    Joined
    Dec 2017
    Posts
    4
    Location

    Toggle SUM/AVG on Pivot Table

    Hi All,

    I am trying to create a toggle for pivot table that will change the Grand Total column from Sum to Average and back again on command. However, I can't get it to select the entire column. Here is the code:


    Sub ToggleCountSum()
        
        Dim pf As PivotField
        
        On Error Resume Next
            Set pf = ActiveCell.PivotField
        On Error GoTo 0
        
        If Not pf Is Nothing Then
            If pf.Function = xlAverage Then
                pf.Function = xlSum
            Else
                pf.Function = xlAverage
            End If
        End If
        
    End Sub
    How can I make it change the whole table instead of the active cell? Also, the pivot table changes in size depending on what splicers i have on (just fyi).

    Thank you!

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    That code will toggle the data field you have selected. I'm not sure how that differs from what you want?
    Be as you wish to seem

  3. #3
    VBAX Newbie
    Joined
    Dec 2017
    Posts
    4
    Location
    The code only toggles for the active cell. I was hoping to toggle for the whole pivot table. Ideally i will have a command button on the adjacent worksheet that i can hit and everything in the grand total column will toggle back and forth from sum to avg.

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    No it doesn't. It will toggle the function for the relevant pivot field and not just a specific cell. A grand total column always uses the same function as its detail columns.
    Be as you wish to seem

  5. #5
    VBAX Newbie
    Joined
    Dec 2017
    Posts
    4
    Location
    Right, I see what you are saying. So is there a way I can toggle the function for all pivot fields? I keep trying to put in a pt reference instead of pf but it does not work:

      Dim pt As PivotTable
        
        On Error Resume Next
            Set pt = ActiveSheet.PivotTable
        On Error GoTo 0

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Some references and structure and objects and logic were wrong


    Option Explicit
    
    Sub ToggleFields()
        Dim pf As PivotField
        
        For Each pf In ActiveSheet.PivotTables(1).DataFields
            If pf.Function = xlAverage Then
                pf.Function = xlSum
            ElseIf pf.Function = xlSum Then
                pf.Function = xlAverage
            End If
        Next
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    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

  7. #7
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    If you want it to work for the currently selected pivot table

    For Each pf In ActiveCell.PivotTable.DataFields
    Be as you wish to seem

  8. #8
    VBAX Newbie
    Joined
    Dec 2017
    Posts
    4
    Location
    Fantastic! Thank you.

Tags for this Thread

Posting Permissions

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