+ Reply to Thread
Results 1 to 18 of 18

Limiting/Hiding PivotItems

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Limiting/Hiding PivotItems

    In my macro I use a basic routine twice to only show a small selection from a vast array of pivot items (Pi and PT are previously defined):
    On Error Resume Next
                For Each Pi In PT.PivotFields("Posn Func").PivotItems
                    Select Case Pi.Name
                    Case "0025", "0058", "0059", "0109", "0110"
                    Case Else
                        Pi.Visible = False
                    End Select
                Next Pi
                On Error GoTo 0
    
    On Error Resume Next
                For Each Pi In PT.PivotFields("Department").PivotItems
                    Select Case Pi.Name
                    Case "510", "511", "513", "514", "516", "517"
                    Case Else
                        Pi.Visible = False
                    End Select
                Next Pi
                On Error GoTo 0
    The oveall macro works just fine, but Extremely Slowly. I have Manual Updating turned to True, and Screen Updating turned off. Walking through the code, it's these two PivotItems sections that are slowing everything down. Is there a faster way to do this?

    Any help is highly appreciated.

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Limiting/Hiding PivotItems

    Can you post a sample wb?

  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: Limiting/Hiding PivotItems

    Not really. The pivot I'm manipulating is in a workbook from our IT group. It's based on a huge datasource, to which I have no acess other than through the pivot. I can doubleclick and pull up some of the data, but it's a truly large set, and includes Names and Pay information. If I were to limit the data I'm sure things would go easier and faster. But I CAN show you how many fields are in the two PivotItem categories; see attached.
    Attached Files Attached Files

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Limiting/Hiding PivotItems

    using pivotfilters should be a lot faster than looping through all the pivotitems
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  5. #5
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: Limiting/Hiding PivotItems

    Jiejenn,
    I tried it your way, no difference in time.

    JosephP,
    Okay, I'll bite; how do we use "pivotfilters" in this case, via VBA?

  6. #6
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Limiting/Hiding PivotItems

    Maybe something like this

    Dim PT As PivotTable
    Dim PTItem As PivotItem
    Set PT = ActiveSheet.PivotTables(1)
    For Each PTItem In PT.PivotFields("Department")
        If PTItem.Name = "510" Or PTItem.Name = "511" Or PTItem.Name = "513" Or PTItem.Name = "514" _
           Or PTItem.Name = "516" Or PTItem.Name = "517" Then
                PTItem.Visible = True
        Else
                PTItem.Visible = False
        End If
    Next PTItem

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Limiting/Hiding PivotItems

    my bad-not enough coffee. the approach I was thinking of is for autofilters and I don't suppose your data source is olap?
    how many items are you looping through? I'm on a cell so can't see your workbook.
    you may be able to speed it up by testing if each item is visible and then only hide it if it is and it's not a department you want.

  8. #8
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: Limiting/Hiding PivotItems

    For departments, there are only 46, of which I want 6.
    Of Posn Func, there are 745, of which I want 4.

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Limiting/Hiding PivotItems

    can you alter the query that pulls from the data source? what is the data source-a db or cube?

  10. #10
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: Limiting/Hiding PivotItems

    Nope, the workbook, including query and datasource, is locked down tight. Literally all I have access to is the pivot table itself. I COULD double-click the Grand Total, and from the page that's generated create a new pivot table, but seems like that would take even longer.

  11. #11
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Limiting/Hiding PivotItems

    ok. have you tried
    -turning off subtotals
    -setting the field sort to manual
    -testing visibility before setting it
    -turning off calculation and events

  12. #12
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: Limiting/Hiding PivotItems

    I guess I should have posted the whole macro to begin with. I've also attached a picture of how the PT looks when I come into it. There are 92 "Par_Program" lines. Each Department has about 70 of those. Each Par_Program has up to 745 Posn-Funcs. So you can see there's a mass of data available. My job in this macro is to whittle it down to two specific views.
    Private Sub CommandButton2_Click()
    Application.ScreenUpdating = False
    Dim Templ8 As Workbook
    Set Templ8 = ThisWorkbook
        Templ8.Sheets("Year-to-Date Paid FTEs").Range("B3:D200").ClearContents
        Templ8.Sheets("Year-to-Date Paid FTEs").Range("G3:I10").ClearContents
        Workbooks.Open _
                ("\\OurServer\SHARE10098\Budget\SOBUDGET\12MFR\Expense_Dtl_Reports\PAID_FTES_BR1112.xlsx"), ReadOnly:=True, UpdateLinks:=True
         On Error Resume Next
    
        Dim PT As PivotTable
        Dim pf As PivotField
    
        Set PT = ActiveSheet.PivotTables(1)
        PT.ManualUpdate = True
    
       
                On Error Resume Next
            For Each Pi In PT.PivotFields("Department")
                If Pi.Name = "510" Or Pi.Name = "511" Or Pi.Name = "513" Or Pi.Name = "514" _
                    Or Pi.Name = "516" Or Pi.Name = "517" Then
                Pi.Visible = True
                Else
                Pi.Visible = False
                End If
            Next Pi
                On Error GoTo 0
            
    
        With PT.PivotFields("Department")
            .Orientation = xlRowField
            .Position = 1
        End With
    
        With PT.PivotFields("PAY_END_DT")
            .Orientation = xlPageField
            .Position = 12
        End With
    
        PT.PivotFields("Department").Subtotals = _
            Array(False, False, False, False, False, False, False, False, False, False, False, False)
        PT.RepeatAllLabels xlRepeatLabels
        PT.RowGrand = False
        PT.ColumnGrand = False
        PT.ManualUpdate = False
    
        PT.DataBodyRange.Select
        
        With PT.TableRange1
        .Offset(2, 0).Resize(.Rows.Count - 2, .Columns.Count).Select
        End With
        Selection.Copy Templ8.Sheets("Year-to-Date Paid FTEs").Range("B3")
        
        PT.ManualUpdate = True
        
                On Error Resume Next
                For Each Pi In PT.PivotFields("Posn Func").PivotItems
                    Select Case Pi.Name
                    Case "0025", "0058", "0059", "0109", "0110"
                    Case Else
                        Pi.Visible = False
                    End Select
                Next Pi
                On Error GoTo 0
                
        PT.ManualUpdate = False
        
        With PT.TableRange1
        .Offset(2, 0).Resize(.Rows.Count - 2, .Columns.Count).Select
        End With
        Selection.Copy Templ8.Sheets("Year-to-Date Paid FTEs").Range("G3")
        
        Workbooks("PAID_FTES_BR1112.xlsx").Close SaveChanges:=False
        
        Application.ScreenUpdating = True
    End Sub
    Attached Images Attached Images

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Limiting/Hiding PivotItems

    this is about all I can think of to try if you can't alter the data source
    
    Private Sub CommandButton2_Click()
        Dim PT               As PivotTable
        Dim pf               As PivotField
        Dim Pi               As PivotItem
        Dim lCalc            As XlCalculation
        Dim wsFTE            As Worksheet
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
            lCalc = .Calculation
            .Calculation = xlCalculationManual
            .DisplayStatusBar = False
        End With
    
        Set wsFTE = ThisWorkbook.Sheets("Year-to-Date Paid FTEs")
    
        With wsFTE
            .Range("B3:D200").ClearContents
            .Range("G3:I10").ClearContents
        End With
    
        Workbooks.Open _
                "\\OurServer\SHARE10098\Budget\SOBUDGET\12MFR\Expense_Dtl_Reports\PAID_FTES_BR1112.xlsx", ReadOnly:=True, UpdateLinks:=True
    
        Set PT = ActiveSheet.PivotTables(1)
    
        With PT
            .ManualUpdate = True
            .RowGrand = False
            .ColumnGrand = False
    
            With .PivotFields("Department")
                .AutoSort xlManual, .SourceName
                .Orientation = xlRowField
                .Position = 1
                .Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
                On Error Resume Next
                For Each Pi In .PivotItems
                    If Pi.Name = "510" Or Pi.Name = "511" Or Pi.Name = "513" Or Pi.Name = "514" _
                       Or Pi.Name = "516" Or Pi.Name = "517" Then
                        If Not Pi.Visible Then Pi.Visible = True
                    Else
                        If Pi.Visible Then Pi.Visible = False
                    End If
                Next Pi
                On Error GoTo 0
                .AutoSort xlAscending, .SourceName
            End With
    
            With .PivotFields("PAY_END_DT")
                .Orientation = xlPageField
                .Position = 12
            End With
    
            .RepeatAllLabels xlRepeatLabels
            .ManualUpdate = False
    
            With .TableRange1
                .Offset(2, 0).Resize(.Rows.Count - 2, .Columns.Count).Copy wsFTE.Range("B3")
            End With
    
            .ManualUpdate = True
    
            On Error Resume Next
            With .PivotFields("Posn Func")
                .AutoSort xlManual, .SourceName
    
                For Each Pi In .PivotItems
                    Select Case Pi.Name
                    Case "0025", "0058", "0059", "0109", "0110"
                    Case Else
                        If Pi.Visible Then Pi.Visible = False
                    End Select
                Next Pi
                On Error GoTo 0
                .AutoSort xlAscending, .SourceName
            End With
            .ManualUpdate = False
    
            With .TableRange1
                .Offset(2, 0).Resize(.Rows.Count - 2, .Columns.Count).Copy wsFTE.Range("G3")
            End With
        End With
        Workbooks("PAID_FTES_BR1112.xlsx").Close SaveChanges:=False
    
        With Application
            .DisplayStatusBar = True
            .EnableEvents = True
            .Calculation = lCalc
            .ScreenUpdating = True
        End With
    End Sub

  14. #14
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: Limiting/Hiding PivotItems

    JosephP,

    Thanks so much. I like the way you rearranged it all; that shows me a few tricks I hadn't thought of. I hope you don't mind if I ask some questions:

    1) What's the purpose of these two lines of code, how do they help?
    .AutoSort xlManual, .SourceName
    .AutoSort xlAscending, .SourceName
    2) I see where you turn Calculation to Manual, but don't see where you turn it back to auto. And what is the meaning of these two lines?
    lCalc = .Calculation
    .Calculation = lCalc
    Thanks,
    John

  15. #15
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Limiting/Hiding PivotItems

    1. turning off sorting on a pivot field is an old trick used when hiding/showing items. I'm not sure if it makes any difference in newer versions of excel but it used to be invaluable in preventing errors.
    2. the first line stores the current calculation setting (since it might not be automatic) and then that setting is restored by the second line. it's better practise than simply resetting to automatic calculation

  16. #16
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: Limiting/Hiding PivotItems

    Followup-The selection for the Posn Func codes isn't happening. I'm walking through the code to find out why.
    Update-had to change this
    Case Else
                        If Pi.Visible Then Pi.Visible = False
                    End Select
    to this
    Case Else
                        Pi.Visible = False
                    End Select


    ---------- Post added at 07:41 AM ---------- Previous post was at 07:33 AM ----------

    JosephP,

    I crossed over your last post, so just read it. Thanks for explaining, especially the 1Calc piece. I've always just returned calculation to automatic; nice to know there's a way to restore it to whatever it was when I came in.

    Thanks for all your help on this. I don't know that we achieved much of a speed difference, but the code is a lot tighter and more efficient now, so I'm pretty happy. Thanks for helping and teaching.
    Last edited by jomili; 08-30-2012 at 08:37 AM.

  17. #17
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Limiting/Hiding PivotItems

    my pleasure. I still have this idea in my head that pivotfilters ought to accept an array of values-as the pivot field dropdown effectively does-without having to loop (in olap pivots you can create a visibleitemslist). maybe in office 16 ;-)

  18. #18
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: Limiting/Hiding PivotItems

    I'm not going to hold my breath, and I suggest you don't either.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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