Pivot Items Filter using VBA

Gaura215

Board Regular
Joined
Feb 2, 2011
Messages
97
HelloI have a code which make a Pivot using VBA. however there is a condition that I am unable to fulfilled in that pivot using my VBA. I will try to write it here the code I wish for.ActiveSheet.PivotTables("PivotTable2").PivotFields_ ("ReportTimeframe") .CurrentPage = "(All)" With ActiveSheet.PivotTables("PivotTable2").PivotFields("Report Timeframe") .PivotItems.Last Month.Visible = True .PivotItems.All Other Months.Visible = False End WithI want a fix in my underline codes above.Any help in this would be appreciable.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Maybe...

Code:
With ActiveSheet.PivotTables("PivotTable2").PivotFields("ReportTimeframe")    
    .PivotItems("Last Month").Visible = True
    .PivotItems("All Other Months").Visible = False
End With
 
Upvote 0
HI Domenic

I think I made a mistake in quoting it in the start. I meant, that I have so many dates coming in the filter. I want the macro to automatically select the dates of the previous months only and leave all other month's dates unchecked.
 
Upvote 0
Try...

Code:
[FONT=Courier New][COLOR=darkblue]With[/COLOR] ActiveSheet.PivotTables("PivotTable2")

    [COLOR=darkblue]With[/COLOR] .PivotFields("ReportTimeframe")
        .DataRange.Cells(1).Group _
            Start:=True, _
            End:=[COLOR=darkblue]True[/COLOR], _
            Periods:=Array(False, False, False, False, [COLOR=darkblue]True[/COLOR], False, True)
        .Orientation = xlPageField
        .CurrentPage = Format(Date, "mmm")
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] .PivotFields("Years")
        .Orientation = xlPageField
        .CurrentPage = Year(Date)
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    [/FONT]

Note, however, if you have a newer version of Excel and the pivot table is not laid out in tabular form, you'll get an error. If this is the case, you can add the following line of code prior to grouping...

Code:
[FONT=Courier New]    [COLOR=darkblue]With[/COLOR] ActiveSheet.PivotTables("PivotTable2")    

        .RowAxisLayout xlTabularRow
        
        [COLOR=green]'[/COLOR]
        '
        '

    End With[/FONT]
 
Upvote 0
This is the code snippet I found on Big Resource and was written by Andrew Poulsom in 2009 who also frequents this forum.
My List:
Employee
MyDate
Tom
01/04/2007
Dirk
01/09/2008
Harry
01/07/2009
Jane
01/05/2010
Joe
01/01/2011
Peter
01/02/2011
Rob
01/03/2011
Irfan
01/04/2011
Julia
01/05/2011
Alice
01/06/2011
Indran
01/03/2012
Lee
01/03/2012

<TBODY>
</TBODY>


My Pivot Table, in the report filter I want to see all records that match a date between 1 Jan to 31 Dec 2011.

MyDate
(Multiple Items)
Count of PersNumber
Employee
Total
Joe
1
Peter
1
Rob
1
Irfan
1
Julia
1
Alice
1
Grand Total
6

<TBODY>
</TBODY>


The VBA code he wrote:
Code:
Sub MultiItemPivotFilter()
    Const StartDate As Date = #1/1/2011#
    Const EndDate As Date = #12/31/2011#
    
    Dim PI As PivotItem
    For Each PI In ActiveSheet.PivotTables("PivotTable1").PivotFields("MyDate").PivotItems
        If DateValue(PI.Name) < StartDate Or DateValue(PI.Name) > EndDate Then
            
Debug.Print DateValue(PI.Name), StartDate, EndDate
            PI.Visible = False
        Else
            PI.Visible = True
        End If
    Next PI
End Sub

Adapt the code to suit your needs. It worked for me.
(Oh and my example list doesnt show the Persnumber column in this post, oops).

:)
 
Upvote 0
Ok I will try this and let you know if this is working fine or if something more/else is required.
 
Upvote 0
R Pele

I tried the following code, but I am getting debug error in the highlighted row. Kindly help me out. It did add all dates falling in the month of may, bt it also selected few dates from march and april as well.

Sub MultiItemPivotFilter()
Const StartDate As Date = #5/1/2012#
Const EndDate As Date = #5/31/2012#

Dim PI As PivotItem
For Each PI In ActiveSheet.PivotTables("PivotTable2").PivotFields("Date Logged").PivotItems
If DateValue(PI.Name) < StartDate Or DateValue(PI.Name) > EndDate Then

Debug.Print DateValue(PI.Name), StartDate, EndDate
PI.Visible = False
Else
PI.Visible = True
End If
Next PI
End Sub
 
Upvote 0
Gaura,
Yes and no.
You can remove the Debug.Print statement, I forgot to remove it, its for testting only.
What I would suggest is you try this code out in a new workbook with a very small set of data, like I did, and step through the code with F8 and hilite the variables to see their values.
As I mentioned, the code came from an MVP and I take no credit for it. I am not a VBA expert at all so I'm not in a position to tell you what else you may try to get the code working other than what I mentioned in the previous sentence. I am assuming this code works in older versions of Excel. I use 2007.
 
Upvote 0
Instead of

If DateValue(PI.Name) < StartDate Or DateValue(PI.Name) > EndDate Then

use the below line

If PI.Name < CStr(StartDate) Or PI.Name > CStr(EndDate) Then

I have used Cstr function as not all the PI.Name values are date type so converted in string to avoid type mismatch error.
Hope it solves your problem .
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top