+ Reply to Thread
Results 1 to 4 of 4

Pivot Table Multiple RowFields Problems

Hybrid View

  1. #1
    Registered User
    Join Date
    04-10-2014
    Location
    Tallahassee, FL
    MS-Off Ver
    Excel 2010
    Posts
    98

    Pivot Table Multiple RowFields Problems

    I have a pivot table with six different Row Labels. (Calling them Cat1, Cat2, etc.)
    Row Labels 1-5 are Expanded, Row Label 6 is Collapsed by default.

    What I am trying to do is go through and and Expand only those Rows in Cat6 that meet a given criteria (in the code below designated "TestObject").

    Sub Test()
        With ActiveSheet.PivotTables("Table")
            Dim pvtfield1, pvtfield2, pvtfield3, pvtfield4, pvtfield5 As PivotField
            Dim pvtitem1, pvtitem2, pvtitem3, pvtitem4, pvtitem5  As PivotItem
            For Each pvtitem4 In .RowFields("Cat5").PivotItems
                For Each pvtitem5 In .PivotFields("Cat6").PivotItems
                        If pvtitem5.Name <> "TestObject" And pvtitem5.RecordCount > 0 Then
                            .PivotFields("Cat5").PivotItems(pvtitem4.Name).ShowDetail = True
                        End If
                Next pvtitem5
            Next pvtitem4
        End With
        Application.StatusBar = Null
    End Sub
    This code is Expanding everything, however, which defeats the purpose.


    Essentially, I need to find a way to pin down the specific combination of Cat1 through Cat5 and specify that particular Cat6 to Expand, but my knowledge is limited.
    Last edited by Phixer; 07-14-2015 at 01:43 PM.

  2. #2
    Registered User
    Join Date
    04-10-2014
    Location
    Tallahassee, FL
    MS-Off Ver
    Excel 2010
    Posts
    98

    Re: Pivot Table Multiple RowFields Problems

    Ok, another attempt that isn't panning out.
    Sub Test()
        With ActiveSheet.PivotTables("PivotTable")
            Dim pvtfield1, pvtfield2, pvtfield3, pvtfield4, pvtfield5 As PivotField
            Dim pvtitem1, pvtitem2, pvtitem3, pvtitem4, pvtitem5, pvtitem6  As PivotItem
            For Each pvtitem1 In .RowFields("Cat1").PivotItems
            For Each pvtitem2 In .RowFields("Cat2").PivotItems
            For Each pvtitem3 In .RowFields("Cat3").PivotItems
            For Each pvtitem4 In .RowFields("Cat4").PivotItems
            For Each pvtitem5 In .RowFields("Cat5").PivotItems
            For Each pvtitem6 In .RowFields("Cat6").PivotItems
    '            On Error Resume Next
                Temp = .GetPivotData("BALANCE", "Cat1", pvtitem1.Name, "Cat2", pvtitem2.Name, "Cat3", pvtitem3.Name, "Cat4", pvtitem4.Name, "Cat5", pvtitem5.Name, "Cat6", pvtitem6.Name).Value
                If pvtitem5.Name <> "TestObject" And .GetPivotData("BALANCE", "Cat1", pvtitem1.Name, "Cat2", pvtitem2.Name, "Cat3", pvtitem3.Name, "Cat4", pvtitem4.Name, "Cat5", pvtitem5.Name, "Cat6", pvtitem6.Name).Value <> 0 Then
                    Stop
                End If
    '            On Error GoTo 0
            Next pvtitem6
            Next pvtitem5
            Next pvtitem4
            Next pvtitem3
            Next pvtitem2
            Next pvtitem1
        End With
        Application.StatusBar = Null
    End Sub
    Keeps breaking on the first .GetPivotData

    Anybody?
    Last edited by Phixer; 07-14-2015 at 08:42 AM.

  3. #3
    Registered User
    Join Date
    04-10-2014
    Location
    Tallahassee, FL
    MS-Off Ver
    Excel 2010
    Posts
    98

    Re: Pivot Table Multiple RowFields Problems

    Ok, so I have tried a slightly different version and it is closer to working than what I was trying. This is closer to my original version.

    Sub Test()
        With ActiveSheet.PivotTables("Table")
            Dim pvtitem4, pvtitem5  As PivotItem
            For Each pvtitem4 In .RowFields("Cat5").PivotItems
                For Each pvtitem5 In .RowFields("Cat6").PivotItems
                        If pvtitem5.Name <> "Test Object" And pvtitem4.RecordCount > 0 And pvtitem5.Visible = True Then
                            .PivotFields("Cat5").PivotItems(pvtitem4.Name).ShowDetail = True
                        End If
                Next pvtitem5
            Next pvtitem4
        End With
    End Sub
    It is still Expanding too many Cat5s, but it isn't expanding ALL of them, so I feel I am on the right track.

    I need to be able to pin it down to check only those pvtitem5s within pvtitem4 and see if there are records in that particular combination to determine if it should expand them or not.

    I need help with the syntax, if someone pleases.

  4. #4
    Registered User
    Join Date
    04-10-2014
    Location
    Tallahassee, FL
    MS-Off Ver
    Excel 2010
    Posts
    98

    Re: Pivot Table Multiple RowFields Problems

    Ok, I tried approaching it in a different manner and this worked:
    Sub Test()
        With ActiveSheet.PivotTables("Table")
            .PivotFields("Cat6").PivotItems("TestObject").Visible = False
            Dim pvtitem4, pvtitem5  As PivotItem
            For Each pvtitem4 In .RowFields("Cat5").PivotItems
                On Error Resume Next
                If pvtitem4.DataRange.Column = Null Then GoTo pvt4
                .PivotFields("Cat5").PivotItems(pvtitem4.Name).ShowDetail = True
    pvt4:
            Next pvtitem4
            .PivotFields("Cat6").PivotItems("TestObject").Visible = True
        End With
    End Sub
    Essentially, it filters for what I want and turns them off. Then it cycles through and looks for those objects which are visible (i.e. do not have anything in their DataRange) and turns them on. Afterwards, it removes the filter and I get exactly what I was needing.

    Hope this helps someone.

+ 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. Need help with Pivot table problems
    By Ephy in forum Excel Charting & Pivots
    Replies: 9
    Last Post: 09-02-2014, 07:25 PM
  2. Pivot Table Rowfields - Values - Email.
    By sarndt01 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-30-2013, 11:49 PM
  3. [SOLVED] Problems with sorting andf Pivot Table problems
    By Saturn in forum Excel General
    Replies: 4
    Last Post: 10-07-2012, 07:47 AM
  4. Pivot Tables: Loop thru RowFields within RowFields
    By davidjones in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-06-2012, 09:22 PM
  5. Pivot Table Problems
    By tompich in forum Excel General
    Replies: 2
    Last Post: 11-21-2007, 06:46 AM
  6. [SOLVED] Pivot table problems
    By [email protected] in forum Excel General
    Replies: 1
    Last Post: 12-12-2005, 10:40 PM
  7. [SOLVED] Pivot Table problems
    By Randy Starkey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-30-2005, 10:25 PM

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