+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Expand & Collapse Pivot Tables in multiple worksheets

  1. #1
    Registered User
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    22

    Expand & Collapse Pivot Tables in multiple worksheets

    Hi,

    I have 5 worksheets, 1, 2, 3 4 & 5
    all have identical Pivot Table columns A, B, C, D & E

    is there a way, using VBA, to expand or collapse the same column, say C, in all 5 worksheets at the same time instead of going into each one manually?

    Thanks

  2. #2
    Valued Forum Contributor dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    New Delhi, India
    MS-Off Ver
    Excel 1997, 2000, XP, 2003, 2007, 2010
    Posts
    2,406

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    Hi Kaseyleigh,

    What do you mean by expand or collapse the column.. ?
    Is it the double click on the right border to auto - adjust or what?


    Regards,
    DILIPandey

    <click on below 'star' if this helps>
    DILIPandey
    +919810929744
    dilipandey@gmail.com

  3. #3
    Registered User
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    Hi,

    In Excel 2010, Pivot Tables now come with Expand / Collapse buttons.

    HTH

    Thanks

  4. #4
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    You want the showdetail property of the pivot field - for example
    Dim ws as worksheet
    dim pt as pivottable
    for each ws in worksheets
    for each pt in ws.pivottables
    ' toggle the third row field to expand
    pt.rowfields("field name").showdetail = True
    next pt
    next ws
    Good luck.

  5. #5
    Registered User
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    Hi Rory,

    Is there a way to use an event so when a use expands a Pivot Table on Sht 1, Col C Row 3, that exact same location in Shts 2, 3, 4 & 5 will also be expanded at the same time. OR collapse depending on what the User did.

    I hope I am making sense.

    Thanks

  6. #6
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    Unfortunately, the events are not that specific - all you will get is notification that the pivot table has changed, not what or why. You would therefore have to iterate through all the PTs and all the fields and reset the detail level, which is not terribly efficient. I also seem to recall having issues with reading the current ShowDetail value but that might be my memory playing tricks, as I haven't tried it for a while.
    Good luck.

  7. #7
    Registered User
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    Oh no!

    Is a way to use ActiveCell and perform the same tasks in the subsequent shts?

    Thanks

  8. #8
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    Let's try it!

    This goes in the ThisWorkbook module of your workbook:

    
    Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Target As PivotTable)
        Dim ptItem As PivotItem
        Dim ptField As PivotField
        Dim pt As PivotTable
        Dim ws As Worksheet
        Dim strField As String
        
        On Error Resume Next
        Set ptItem = ActiveCell.PivotItem
        On Error GoTo Oops
        
        If Not ptItem Is Nothing Then
            Set ptField = ptItem.Parent
            With ptField
                If .Orientation = xlColumnField Or .Orientation = xlRowField Then
                    With Application
                        .ScreenUpdating = False
                        .EnableEvents = False
                    End With
                    strField = .Name
                    On Error Resume Next
                    For Each ws In ActiveWorkbook.Worksheets
                        For Each pt In ws.PivotTables
                            If Not pt.TableRange1.Address(external:=True) = _
                                    Target.TableRange1.Address(external:=True) Then
                                pt.PivotFields(strField).PivotItems(ptItem.Caption).ShowDetail = ptItem.ShowDetail
                            End If
                        Next pt
                    Next ws
                    On Error GoTo Oops
                End If
            End With
        End If
        
    clean_up:
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        Exit Sub
        
    Oops:
        MsgBox Err.Description
        Resume clean_up
    
    End Sub
    Good luck.

  9. #9
    Registered User
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    Hey Rory,

    That's it! You're a Genius!

    Thank you! Thank you!

    Please may I ask you to tell me what each line of your code does, so I can understand it?


    Thank you once again for your assistance.

  10. #10
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    Here is a commented version - hopefully it is clear
    Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Target As PivotTable)
        Dim ptItem As PivotItem
        Dim ptField As PivotField
        Dim pt As PivotTable
        Dim ws As Worksheet
        Dim strField As String
        
        On Error Resume Next
        ' checks the active cell is in a pivot table
        Set ptItem = ActiveCell.PivotItem
        On Error GoTo Oops
        
        If Not ptItem Is Nothing Then
            ' get the pivot field for the active cell
            Set ptField = ptItem.Parent
            With ptField
                ' check if it's a row or colulmn field
                ' if it isn't, it can't be expanded so no point doing anything
                If .Orientation = xlColumnField Or .Orientation = xlRowField Then
                    ' stop screen updates to speed things up
                    '
                    ' and turn off events so that this code doesn't get triggered
                    ' again as we alter each pivot table
                    With Application
                        .ScreenUpdating = False
                        .EnableEvents = False
                    End With
                    ' store name of the pivot field
                    strField = .Name
                     ' lazy error handling - just ignores any errors
                    ' such as the field not being opresent in one of the tables
                   On Error Resume Next
                   ' loop through all worksheets and pivot tables
                    For Each ws In ActiveWorkbook.Worksheets
                        For Each pt In ws.PivotTables
                            ' check the pivot table in question is not
                            ' the one the user just altered - no need to process that one!
                            If Not pt.TableRange1.Address(external:=True) = _
                                    Target.TableRange1.Address(external:=True) Then
                                    ' expand or contract the item as applicable
                                pt.PivotFields(strField).PivotItems(ptItem.Caption).ShowDetail = ptItem.ShowDetail
                            End If
                        Next pt
                    Next ws
                    ' reset error handler
                    On Error GoTo Oops
                End If
            End With
        End If
        
    clean_up:
        ' this makes sure we cannot exit without turning events back on
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        Exit Sub
        
    Oops:
        ' this is the error handler
        MsgBox Err.Description
        Resume clean_up
    
    End Sub
    Good luck.

  11. #11
    Registered User
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    Thank you for going to all the trouble.

    I wouldn't have known where to begin.

    TY !

  12. #12
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    My pleasure.
    Good luck.

  13. #13
    Registered User
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    Hi Rory,

    I need your assistance once again, to add something to your code, because I am getting a "Type mismatch" error when I refresh a Pivot Table

    Something like, if pt.RefreshTable exit sub. My novice attempt

    Thank you!

  14. #14
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    Any chance you can post the workbook?

    There aren't many lines there that aren't in an On Error Resume Next block so I'm struggling to see where that error might occur, but worst case you could try removing the two
    on error goto Oops
    lines
    Good luck.

  15. #15
    Registered User
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Expand & Collapse Pivot Tables in multiple worksheets

    Taking a look at your code, my thoughts are >>
    Your code is looking for a ptField and if it's not "On Error GoTo ....". There will always be a ptField but the action I am performing is a Refresh not Expand / Collapse. Should the "On Error GoTo ...." be looking at something else? Am I heading in the wrong direction?

    The workbook is massive, I will put together a smaller version and post tomorrow.

+ 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.2.0