+ Reply to Thread
Results 1 to 18 of 18

Macro to toggle Pivot - Collapse/Expand entire field

  1. #1
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Macro to toggle Pivot - Collapse/Expand entire field

    Hey guys

    Need your help with VBA and pivottables.

    I want to select a field in a pivottable, and then be able to click a macro button to toggle the "collapse/expand" entire field. I need a way to reference the pivot field represented by the cell that has been selected.

    (Post edited and removed unnecessary info).
    Last edited by rasonline; 08-06-2017 at 02:39 AM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    Try something like this (not tested).

    Please Login or Register  to view this content.
    You may find this link helpful:
    Referencing an Excel Pivot Table Range using VBA
    Last edited by AlphaFrog; 08-01-2017 at 11:40 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    thanks for the reply. Unforutnately did not work.

    When I place the cell outside the pivottable, then the macro executes but with no result or change on the page
    I know this because I added "msgbox Done" at the end, and it does this.

    when I select a cell inside the pivottable, then I get a DEBUG error on this line
    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    note that this line of code was also generating an error, so I quoted it out

    Please Login or Register  to view this content.

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    What exactly are you selecting before you run the macro? Your description of events is confusing and sometimes contradictory.

    Quote Originally Posted by rasonline View Post
    • I want to select a field in a pivottable,...
    • How do I replace the PivotFields("Project Name") with something that will be based on the active cell that the user selects in the pivottable?
    • So, in practice, I will select a cell in the pivottable, ...
    • I only need help with the code to select the pivotfield.
    • When I place the cell outside the pivottable, then the macro executes but with no result or change on the page

  6. #6
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    Sorry for the confusion.

    What I need to achieve - I click a cell inside a pivot table. Then click a macro button and it should collapse/expand the entire field. That's it.

    I should be able to use this macro on any pivot table, in any workbook.


    (Ignore references in previous post about clicking outside pivot table. That was only for purposes of diagnosing why your code did not work)

  7. #7
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    The pivot table display is Classic view, so each field is in a separate column.

  8. #8
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    excelquery.png

    here is a screenshot to demonstrate the request.
    Want to place the cursor in the first column, then click a button and collapse all groups.

  9. #9
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    Help... ok. Here is the most simple way of describing it. I need a macro to do the same as the excel command -
    expand.collapse.jpg

    I want to assign it to button on my Quick Access Toolbar.

  10. #10
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    Ok - i've managed to solve it. Here is the solution for anybody else who wants it.
    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  12. #12
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2507 (Windows 11 Home 24H2 64-bit)
    Posts
    91,792

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    Rasonline - please read the forum rules on cross-posting and make sure that you comply with them in future: https://www.excelforum.com/forum-rul...rum-rules.html
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  13. #13
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    Thanks, noted and will apply for future reference.

  14. #14
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    I think you could shorten it to this for what it's worth.

    ActiveCell.PivotField.ShowDetail = False

  15. #15
    Registered User
    Join Date
    04-03-2017
    Location
    Auckland, New Zealand
    MS-Off Ver
    2010
    Posts
    12

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    Quote Originally Posted by rasonline View Post
    Ok - i've managed to solve it. Here is the solution for anybody else who wants it.
    Please Login or Register  to view this content.
    Although the "PfName" code is helpful, and is at least as concise as any other I've found thanks, that code doesn't actually toggle the "collapse/expand" function; it simply collapses the selected field.
    All the research I've done hasn't come up with a one-button toggle function, so it seems the next best option is to have a button where "ShowDetail = False" and another where "ShowDetail = True".
    There is always a better way.

  16. #16
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    Resurrecting this thread - trying this again in 2024.

    Why would the messagebox in this code give a FALSE???
    The code successfully expands the pivottable field! So the field properly is definitely TRUE.

    But trying to capture that property as a variable still yields FALSE....??
    I even tried using "As Long" but unable to extract a TRUE or 1.

    Please Login or Register  to view this content.



    _________________________________________________________________________________________________________
    Ideally, I want to create a toggle button like this (as opposed to the standard separate expand and collapse buttons):

    Please Login or Register  to view this content.
    Any suggestions?
    Last edited by rasonline; 03-08-2024 at 07:59 AM.

  17. #17
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2507 (Windows 11 Home 24H2 64-bit)
    Posts
    91,792

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    The thread is marked as solved - remove that if you want further assistance.

  18. #18
    Forum Contributor
    Join Date
    07-24-2008
    Location
    Athome
    MS-Off Ver
    Excel 2013
    Posts
    127

    Re: Macro to toggle Pivot - Collapse/Expand entire field

    Quote Originally Posted by AliGW View Post
    The thread is marked as solved - remove that if you want further assistance.
    thanks for that. Now updated thread title to remove "solved"

+ 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. [SOLVED] Collapse expand Pivot
    By EXLent in forum Excel Charting & Pivots
    Replies: 10
    Last Post: 03-15-2016, 06:08 PM
  2. [SOLVED] Adapting VBA code to alow use of expand/collapse field on a pivot table
    By michelle 1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-06-2015, 07:37 AM
  3. Multiple Collapse/Expand - Pivot Table Field List
    By pattyjay in forum Excel General
    Replies: 2
    Last Post: 09-17-2015, 04:37 PM
  4. Expand/Collapse Entire Items in a Field in Excel 2007
    By ExcelTip in forum Tips and Tutorials
    Replies: 5
    Last Post: 06-08-2015, 07:52 AM
  5. [SOLVED] Pivot T Problem w/Expand-Collapse all PTs
    By catnam in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 10-22-2014, 08:46 AM
  6. how to stop Expand Entire Field when Refresh pivot table
    By ttulinsky in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 07-03-2013, 01:02 PM
  7. macro to expand and collapse a pivot field failing.
    By dcgrove in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-01-2010, 02:30 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