Ask Experts Questions for FREE Help !
Ask
    jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #1

    Mar 15, 2009, 12:57 PM
    Hiding PivotItems using VBA
    I have built a simple macro using VBA which hides certain dates from view in my pivot table. For example, I have date values like: 1/1/2007, 1/5/2007, 2/15/2007, 5/1/2008, 2/1/2009. I've been able create my macro to hide dates that I specify in the below code:

    Sub Macro1()
    With ActiveSheet.PivotTables("PivotTable1").PivotFields ("Date")
    .PivotItems("1/2/2007").Visible = False
    .PivotItems("2/15/2007").Visible = False
    .PivotItems("5/1/2008").Visible = False

    End With
    End Sub

    My problem is that sometimes I have to refresh the data behind the pivot table and the dates can change but I will want to exclude certain dates from appearing in my pivot table results. For instance, I might want to exclude all dates prior to February 2007 or see only those dates that appear after January 2009. I can go into the pivot table and just de-select the dates but it's very tedious.

    Can I create code that will de-select all dates prior to a given period? For example, if I had dates throughout January and February 2009 and I only wanted to see February dates, can I make the code do something like hide all date values <2/1/2009? Is that possible without having to go into the pivottable and manually de-select all January dates?

    Hope this makes sense.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #2

    Mar 15, 2009, 09:28 PM

    This page shows a technique for comparing NOW() to the .PivotItems and hiding the ones that result in "false" on the test.

    Hide Show Pivot Column Items Based on Date - Excel Help Best Practices Forums
    Code:
    Sub Macro1() 
         '
        Dim pvtItem As PivotItem 
        Dim strDate As String 
         
         ' check  date  format - especially single digit days 01 or 1
         
        strDate = Format(Now(), "mm/dd/yyyy") 
        With ActiveSheet. PivotTables("PivotTable1").PivotFields("Date") 
            .ShowAllItems = True '
            For Each pvtItem In .PivotItems 
                pvtItem.Visible = (pvtItem. Name = strDate) 
            Next 
        End With 
    End Sub
    You could put that date to filter by in a CELL (A1? ) and adjust the formula to be greater than instead of equal:
    Code:
    Sub Macro1() 
         '
        Dim pvtItem As PivotItem 
        Dim strDate As String
         
         ' check  date  format - especially single digit days 01 or 1
         
        strDate = Format(Range("A1").Value, "m/d/yyyy") 
        With ActiveSheet. PivotTables("PivotTable1").PivotFields("Date") 
            .ShowAllItems = True '
            For Each pvtItem In .PivotItems 
                pvtItem.Visible > (pvtItem. Name = strDate) 
            Next 
        End With 
    End Sub
    This should give you enough to "fiddle with".
    jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #3

    Mar 16, 2009, 10:17 AM
    Quote Originally Posted by JBeaucaire View Post
    This page shows a technique for comparing NOW() to a the .PivotItems and hiding the ones that result in "false" on the test.

    Hide Show Pivot Column Items Based on Date - Excel Help Best Practices Forums
    Code:
    Sub Macro1() 
         '
        Dim pvtItem As PivotItem 
        Dim strDate As String 
         
         ' check  date  format - especially single digit days 01 or 1
         
        strDate = Format(Now(), "mm/dd/yyyy") 
        With ActiveSheet. PivotTables("PivotTable1").PivotFields("Date") 
            .ShowAllItems = True '
            For Each pvtItem In .PivotItems 
                pvtItem.Visible = (pvtItem. Name = strDate) 
            Next 
        End With 
    End Sub
    You could put that date to filter by in a CELL (A1 ??) and adjust the formula to be greater than instead of equal:
    Code:
    Sub Macro1() 
         '
        Dim pvtItem As PivotItem 
        Dim strDate As String
         
         ' check  date  format - especially single digit days 01 or 1
         
        strDate = Format(Range("A1").Value, "m/d/yyyy") 
        With ActiveSheet. PivotTables("PivotTable1").PivotFields("Date") 
            .ShowAllItems = True '
            For Each pvtItem In .PivotItems 
                pvtItem.Visible > (pvtItem. Name = strDate) 
            Next 
        End With 
    End Sub
    This should give you enough to "fiddle with".
    Thanks, guy... I've give you a "greenie" but I got to spread some of the love around. I think this will work so I will definitely "fiddle with" it.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search


Check out some similar questions!

Hiding a Tattoo [ 8 Answers ]

This one is for all you tattooed people out there! I was wondering if anyone knew of a makeup that would cover up a tattoo? My fiancés parents are sort of conservative... LOL and needless to say they don't know about my body art. So... anything out there on the market that can hide my secret?...

Is she hiding something [ 1 Answers ]

Me and my friend Raven have... or HAD a really close friend and all of a sudden she just started acting really different. I mean she doesn't really talk to us as much, she doesn't goof off with us anymore, she doesn't even laugh at anything funny we do. We really feel like she is hiding something...

Hiding money [ 1 Answers ]

How to send and get money with out records

Running and in hiding [ 1 Answers ]

I have been collecting NJ DMV tickets since I've been 19 yrs old, I'm now 30. I had a run in with the law when I was 24 ( Theft charge ). Ok so this is my problem. I have about 19 DMV warrants, and I'm looking at my 3rd offense on my violation of probation. Recently I was charged with Fraud. I...

8 months. Old cat, hiding [ 2 Answers ]

My family just purchased an 8 month old Maine Coon cat. He had been living in an apartment with 3 adults, his parents and 2 other cats. He now spends the day under my daughter's bed and at night when we're sleeping, eats and uses the litter box. He lets us pet him when he's under the bed and has...


View more questions Search