Filter only results in the next 28 days from Pivot Table using VBA

dannybland

New Member
Joined
Sep 12, 2014
Messages
31
Hi,

I am looking to filter my table to show only the data from the next 28 days, I tried using the below but it returned an error. Included full example below

pt.PivotFields("Dates").PivotFilters.Add Type:=xlDateBetween, Value1:=Today(), Value2:=Today() + 28



Sub PTFour()


Sheets.Add
ActiveSheet.Name = "x"

Dim pt As PivotTable
Dim strField As String
Dim WSD As Worksheet
Set WSD = Worksheets("Raw Data")
Dim PTOutput As Worksheet
Set PTOutput = Worksheets("x")
Dim PTCache As PivotCache
Dim PRange As Range

' Find the last row with data
Dim finalRow As Long
finalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row

' Find the last column with data
Dim finalCol As Long
finalCol = WSD.Cells(1, Application.Columns.Count).End(xlToLeft).Column

' Find the range of the data
Set PRange = WSD.Cells(1, 1).Resize(finalRow, finalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange)

' Create the pivot table
Set pt = PTCache.CreatePivotTable(TableDestination:=PTOutput.Cells(1, 1), _
TableName:="EquityInvestmentPivot")

' Define the layout of the pivot table

' Set update to manual to avoid recomputation while laying out
pt.ManualUpdate = True

'Setting Fields
With pt

'set row field
With .PivotFields("Make/Model")
.Orientation = xlRowField
.Position = 1
End With


'set column field
With .PivotFields("Company Code")
.Orientation = xlColumnField
.Position = 1
End With


'set data field
.AddDataField .PivotFields("Remaining Equity Investment"), "Sum of Equity Invested", xlSum
End With

With ActiveSheet.PivotTables("EquityInvestmentPivot").PivotFields( _
"Sum of Equity Invested")
.NumberFormat = "£#,##0.00;[Red]-£#,##0.00"
End With

pt.PivotFields("Dates").PivotFilters.Add Type:=xlDateBetween, Value1:=Today(), Value2:=Today() + 28

' Now calc the pivot table
pt.ManualUpdate = False


ActiveSheet.PivotTables("EquityInvestmentPivot").TableStyle2 = _
"PivotStyleMedium4"
ActiveWorkbook.ShowPivotTableFieldList = False


MsgBox "Please see your requested pivot table. If you require another, please go back to the Pivot Table Selection tab."

End Sub
 
That's not what I had in my code.

Rich (BB code):
PI.Visible = (DateValue(PI.Value) >= Date And DateValue(PI.Value) <= Date + 28)
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
No, still pulls all data.

Code:
    Dim Fmt As String    Dim PI As PivotItem
    With ActiveSheet.PivotTables("ExpiringEquityPivot").PivotFields("Termination Date")
        .ClearAllFilters
        Fmt = .NumberFormat
        .NumberFormat = "dd/mm/yyyy"
        For Each PI In .PivotItems
            Debug.Print CDate(PI.Value)
            PI.Visible = (DateValue(PI.Value) >= Date And DateValue(PI.Value) <= Date + 28)
        Next PI
        .NumberFormat = Fmt
    End With

Also, yes based in UK
 
Last edited:
Upvote 0
Make/ModelTermination DateCompany CodeRemaining Equity InvestmentFunding StatusDetailed Funding Status
LEXUS IS06/09/2015COS0Fully FundedFully Funded (<12 Months)
AUDI A502/03/2014COS143.17Part FundedPart Funded (<12 Months)
AUDI A415/09/2013CBF5929.11Part FundedPart Funded (<12 Months)
VOLVO V6017/03/2015CBF2219.87Part FundedPart Funded (<12 Months)
BMW 511/07/2015CBL0Funder To Be AllocatedFunder To Be Allocated (<12 Months)
AUDI A510/11/2013CBL0Fully FundedFully Funded (<12 Months)
VOLVO V4001/02/2017CBF497.76Part FundedPart Funded (<36 Months)
BMW 5 SERIES20/12/2015CBF6532.34Part FundedPart Funded (<24 Months)
AUDI A105/10/2014COS1383.51Part FundedPart Funded (<12 Months)
TOYOTA YARIS31/01/2015COS0Fully FundedFully Funded (<12 Months)
AUDI A317/01/2015COS1737.5Part FundedPart Funded (<12 Months)
AUDI A303/09/2014COS1870.2Part FundedPart Funded (<12 Months)
MERCEDES-BENZ C CLASS03/01/2017CBF702.72Part FundedPart Funded (<36 Months)
AUDI Q312/12/2014COS0Fully FundedFully Funded (<12 Months)
VOLKSWAGEN POLO07/01/2015COS0Fully FundedFully Funded (<12 Months)
VOLKSWAGEN POLO28/01/2015COS0Fully FundedFully Funded (<12 Months)
AUDI A311/03/2015COS0Fully FundedFully Funded (<12 Months)
AUDI A117/09/2014COS0Fully FundedFully Funded (<12 Months)
MERCEDES-BENZ E CLASS01/09/2014COS187.69Dealer FundedDealer Funded (<12 Months)
VOLKSWAGEN GOLF23/12/2014COS0Fully FundedFully Funded (<12 Months)
NISSAN NOTE27/03/2015COS0Fully FundedFully Funded (<12 Months)
AUDI Q328/09/2014COS22301.45Self Funded (<12 Months)Self Funded (<12 Months)
LEXUS RX06/09/2015COS0Fully FundedFully Funded (<12 Months)
LAND ROVER DISCOVERY28/09/2014COS4669.88Part FundedPart Funded (<12 Months)
SEAT LEON16/04/2015COS689.87Part FundedPart Funded (<12 Months)
AUDI A303/10/2014COS0Fully FundedFully Funded (<12 Months)
VOLKSWAGEN PASSAT ALLTRACK10/04/2015COS0Fully FundedFully Funded (<12 Months)
TOYOTA YARIS01/03/2015COS0Fully FundedFully Funded (<12 Months)
VOLKSWAGEN GOLF25/10/2014COS175.8Part FundedPart Funded (<12 Months)
NISSAN JUKE10/04/2015COS0Fully FundedFully Funded (<12 Months)
RENAULT CLIO23/04/2015COS10418.93Self Funded (<12 Months)Self Funded (<12 Months)
NISSAN JUKE30/04/2015COS0Fully FundedFully Funded (<12 Months)
RENAULT CLIO30/04/2015COS0Fully FundedFully Funded (<12 Months)
TOYOTA AURIS04/09/2015COS0Fully FundedFully Funded (<12 Months)
TOYOTA YARIS15/08/2015COS0Fully FundedFully Funded (<12 Months)
NISSAN JUKE01/09/2015COS0Fully FundedFully Funded (<12 Months)
VOLKSWAGEN SCIROCCO21/05/2015COS1926.4Part FundedPart Funded (<12 Months)
SEAT LEON30/01/2015COS14859.85Self Funded (<12 Months)Self Funded (<12 Months)
TOYOTA AURIS01/09/2015COS0Fully FundedFully Funded (<12 Months)
SKODA CITIGO11/07/2015COS1139.19Part FundedPart Funded (<12 Months)
SKODA OCTAVIA28/05/2015COS17429.37Self Funded (<12 Months)Self Funded (<12 Months)
AUDI A330/05/2015COS666.95Part FundedPart Funded (<12 Months)
AUDI Q313/06/2015COS0Fully FundedFully Funded (<12 Months)
AUDI A519/12/2014COS59.67Part FundedPart Funded (<12 Months)
VOLKSWAGEN UP01/07/2015COS0Fully FundedFully Funded (<12 Months)
AUDI A308/02/2015COS19960.28Funder To Be AllocatedFunder To Be Allocated (<12 Months)
AUDI A101/08/2015COS0Fully FundedFully Funded (<12 Months)
TOYOTA AURIS01/09/2015COS0Fully FundedFully Funded (<12 Months)
AUDI A301/03/2015COS29877.78Funder To Be AllocatedFunder To Be Allocated (<12 Months)
AUDI A305/03/2015COS16533.2Funder To Be AllocatedFunder To Be Allocated (<12 Months)
AUDI A301/03/2015COS19613.9Funder To Be AllocatedFunder To Be Allocated (<12 Months)
NISSAN JUKE17/09/2014COS0Fully FundedFully Funded (<12 Months)
VAUXHALL INSIGNIA19/11/2016CBL0Fully FundedFully Funded (<36 Months)
VAUXHALL INSIGNIA16/08/2014CBL0Fully FundedFully Funded (<12 Months)
VAUXHALL INSIGNIA21/06/2014CBF9231.74Part FundedPart Funded (<12 Months)
VOLKSWAGEN POLO22/11/2014COS10150.23Self Funded (<12 Months)Self Funded (<12 Months)
TOYOTA AURIS06/06/2015COS0Fully FundedFully Funded (<12 Months)
AUDI A402/11/2014COS0Fully FundedFully Funded (<12 Months)
LAND ROVER DISCOVERY20/12/2014COS0Dealer FundedDealer Funded (<12 Months)
VOLKSWAGEN GOLF07/02/2015COS0Fully FundedFully Funded (<12 Months)
AUDI Q523/12/2014COS76.16Part FundedPart Funded (<12 Months)
NISSAN JUKE30/05/2015COS0Fully FundedFully Funded (<12 Months)
AUDI A101/09/2015COS16561.13Funder To Be AllocatedFunder To Be Allocated (<12 Months)
AUDI A603/03/2015COS42136.85Funder To Be AllocatedFunder To Be Allocated (<12 Months)
AUDI Q501/02/2015COS0Fully FundedFully Funded (<12 Months)
AUDI Q505/03/2015COS34966Funder To Be AllocatedFunder To Be Allocated (<12 Months)
VOLKSWAGEN SCIROCCO14/03/2015COS0Fully FundedFully Funded (<12 Months)
VOLKSWAGEN UP21/11/2014COS1455.49Part FundedPart Funded (<12 Months)
AUDI A501/04/2015CBF21170.44Self Funded (<12 Months)Self Funded (<12 Months)
AUDI A327/12/2014COS316.57Part FundedPart Funded (<12 Months)
TOYOTA AURIS15/07/2015COS0Fully FundedFully Funded (<12 Months)
AUDI A630/12/2014COS0Dealer FundedDealer Funded (<12 Months)
AUDI A101/09/2015COS0Fully FundedFully Funded (<12 Months)
AUDI A611/01/2015COS29184.8Funder To Be AllocatedFunder To Be Allocated (<12 Months)
AUDI TT28/07/2014COS0Fully FundedFully Funded (<12 Months)
LAND ROVER RANGE ROVER10/10/2014COS0Fully FundedFully Funded (<12 Months)
BMW 1 SERIES07/06/2016CBF1047.36Part FundedPart Funded (<24 Months)
NISSAN MICRA24/10/2014COS8091.66Self Funded (<12 Months)Self Funded (<12 Months)
LAND ROVER RANGE ROVER SPORT02/07/2014COS93.42Part FundedPart Funded (<12 Months)
AUDI A117/10/2014COS0Fully FundedFully Funded (<12 Months)
SEAT LEON04/11/2014COS2067.02Part FundedPart Funded (<12 Months)
LAND ROVER DISCOVERY13/04/2016CBL0Fully FundedFully Funded (<24 Months)
SEAT IBIZA30/09/2014COS0Fully FundedFully Funded (<12 Months)
SKODA SUPERB16/03/2015CBL20874.99Funder To Be AllocatedFunder To Be Allocated (<12 Months)
VOLVO V5023/05/2016CBF1498.52Part FundedPart Funded (<24 Months)
VOLKSWAGEN POLO16/10/2014COS0Fully FundedFully Funded (<12 Months)
AUDI A313/06/2016CBF12300Self Funded (<24 Months)Self Funded (<24 Months)
NISSAN NOTE27/03/2015COS0Fully FundedFully Funded (<12 Months)
NISSAN NOTE19/12/2014COS885.03Part FundedPart Funded (<12 Months)
MERCEDES-BENZ E CLASS01/10/2014COS0Fully FundedFully Funded (<12 Months)
SKODA OCTAVIA28/04/2015COS19130.5Self Funded (<12 Months)Self Funded (<12 Months)
KIA SORENTO26/08/2018CBF26153.57Funder To Be AllocatedFunder To Be Allocated (<48 Months)
VOLKSWAGEN GOLF24/09/2017CBF20618.31Self Funded (<48 Months)Self Funded (<48 Months)
NISSAN JUKE24/09/2014COS0Fully FundedFully Funded (<12 Months)
VOLKSWAGEN POLO09/12/2014COS1054.65Part FundedPart Funded (<12 Months)
MERCEDES-BENZ C26/01/2013CBL1Funder To Be AllocatedFunder To Be Allocated (<12 Months)
AUDI A701/06/2016CBF7229.96Part FundedPart Funded (<24 Months)
HONDA CIVIC01/06/2016CBF1049.28Part FundedPart Funded (<24 Months)
LAND ROVER RANGE ROVER EVOQUE25/09/2016CBF20783.83Self Funded (<36 Months)Self Funded (<36 Months)
FORD MONDEO09/07/2016CBL0Fully FundedFully Funded (<24 Months)

<colgroup><col><col><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
If I run my VBA code on a pivot table created from that data I get:


Excel 2010
HIJ
1Termination Date(Multiple Items)
2
3Sum of Remaining Equity InvestmentColumn Labels
4Row LabelsCOSGrand Total
5AUDI A11383.511383.51
6AUDI A300
7AUDI Q322301.4522301.45
8LAND ROVER DISCOVERY4669.884669.88
9LAND ROVER RANGE ROVER00
10MERCEDES-BENZ E CLASS00
11NISSAN JUKE00
12SEAT IBIZA00
13Grand Total28354.8428354.84
Sheet1
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,543
Members
449,089
Latest member
davidcom

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