+ Reply to Thread
Results 1 to 10 of 10

Hide and show pivotfields with vba

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-24-2014
    MS-Off Ver
    Microsoft Office 2013
    Posts
    113

    Hide and show pivotfields with vba

    I have a pivot chart that I would like to make more interactive and allow the user to select which data they would like to see. I didn't know the code to do this so I recorded a macro of when I changed what was seen. I took one off, then brought it back and changed it so it would average the data. I then tried to run through the macro and it kept giving me an error. I have posted my code below. Once I can get the basics done on this I will go ahead and use if formulas with my checkboxes to make it work how I would like. Any help would be great.

    Sub Macro15()
    '
    ' Macro15 Macro
    
        ActiveSheet.ChartObjects("Chart 1").Activate
        ActiveWorkbook.ShowPivotTableFieldList = True
        ActiveChart.PivotLayout.PivotTable.PivotFields("Documented in Excel"). _
            Orientation = xlHidden
        ActiveWorkbook.ShowPivotTableFieldList = False
        
        ActiveSheet.ChartObjects("Chart 1").Activate
        ActiveWorkbook.ShowPivotTableFieldList = True
        ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
            PivotTable.PivotFields("Documented in Excel"), "Sum of Documented in Excel", _
            xlSum
        With ActiveChart.PivotLayout.PivotTable.PivotFields( _
            "Sum of Documented in Excel")
            .Caption = "Average of Documented in Excel"
            .Function = xlAverage
        End With
        ActiveWorkbook.ShowPivotTableFieldList = False
    End Sub
    Last edited by manofcheese; 05-29-2014 at 03:18 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Hide and show pivotfields with vba

    Can you provide a sample workbook?

  3. #3
    Forum Contributor
    Join Date
    05-24-2014
    MS-Off Ver
    Microsoft Office 2013
    Posts
    113

    Re: Hide and show pivotfields with vba

    Example Spreadsheet.xlsmI hope this attachment works. I just made a quick example of basically what I am trying to do. This is my first time using the forum. Thanks for any help you can give.

  4. #4
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Hide and show pivotfields with vba

    Took me a while of messing around with it, but this should work:
    Sub Test()
    '
    ' Test Macro
    ' This is supposed to hide data 1 and then bring it back. Once I can do that I will modify it to work how I would like.
    '
    
    Dim tbl As PivotTable
    Set tbl = Sheets("Pivot Table").PivotTables(1)
    With tbl
        .PivotFields("Average of Data 1").Orientation = xlHidden
        With .PivotFields("Data 1")
            .Orientation = xlDataField
            .Name = "Average of Data 1"
            .Function = xlAverage
        End With
    
    End With
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    05-24-2014
    MS-Off Ver
    Microsoft Office 2013
    Posts
    113

    Re: Hide and show pivotfields with vba

    Thank you so much. I had looked for hours trying to figure this out. I modified the code to work with a check box. It worked like a charm
    Private Sub CheckBox1_Click()
    Application.ScreenUpdating = False
    Dim tbl As PivotTable
    Set tbl = Sheets("Pivot Table").PivotTables(1)
    
    If Sheets("Pivot Chart").CheckBox1.Value = False Then
    With tbl
        .PivotFields("Average of Data 1").Orientation = xlHidden
    End With
    End If
    If Sheets("Pivot Chart").CheckBox1.Value = True Then
    With tbl
        With .PivotFields("Data 1")
            .Orientation = xlDataField
            .Name = "Average of Data 1"
            .Function = xlAverage
        End With
    
    End With
    End If
    Application.ScreenUpdating = True
    End Sub

  6. #6
    Forum Contributor
    Join Date
    05-24-2014
    MS-Off Ver
    Microsoft Office 2013
    Posts
    113

    Re: Hide and show pivotfields with vba

    So now I am trying to apply this to my chart at work and I am having trouble with the
    Set tbl = Sheets("Pivot Table").PivotTables(1)
    part of the formula. When setting it on another chart what property should I change and how would I find it?

  7. #7
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Hide and show pivotfields with vba

    Awesome! Glad I could help.

  8. #8
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Hide and show pivotfields with vba

    That code selects the 1st pivot table on the sheet "Pivot Table". Make sure the sheet name is right. Is your chart using a pivot table as a data source? If so, you can just change the pivot table and the chart will change. Otherwise you can do something like Sheets("Sheet Name").Charts(1) or Sheets("Sheet Name").Charts("Chart Name")

  9. #9
    Forum Contributor
    Join Date
    05-24-2014
    MS-Off Ver
    Microsoft Office 2013
    Posts
    113

    Re: Hide and show pivotfields with vba

    You are a genius. Thank you again.

  10. #10
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Hide and show pivotfields with vba

    You're more than welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Show/Hide Shape VBA
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-06-2013, 09:03 AM
  2. VBA hide row based on pull down with button to toggle show/hide
    By myronr in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-12-2012, 06:07 PM
  3. Pivotfields & Pivotitems VBA
    By pickledmuffin in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-08-2008, 06:12 PM
  4. [SOLVED] PivotFields problem
    By Tim in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-17-2005, 01:35 PM
  5. [SOLVED] Hide and show Rows
    By Duane in forum Excel General
    Replies: 2
    Last Post: 04-19-2005, 06:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1