PDA

View Full Version : Solved: Pivot Date Filter between variable number of days



Phelony
10-18-2012, 05:01 AM
Hi guys

I'm trying to make a quick filter for a pivot table that will show only the items between today and the number of days specified in the input box. :dunno

Private Sub CommandButton1_Click()
Dim pvtItem As PivotItem
Dim strDate As Date
Dim pTable As String
Dim pField As String
Dim dRange As Long

'sheet variables
pTable = "PivotTable5"
pField = "Date"

dRange = InputBox("Enter the number of days ahead of today you would like to view as a number. (e.g 6 months = 180)")

strDate = Format(Now(), "dd/mm/yyyy")

With ActiveSheet.PivotTables(pTable).PivotFields(pField)
.ShowAllItems = True '
For Each pvtItem In .PivotItems

'should show dates between today and today + the number entered in the input box
pvtItem.Visible = (pvtItem.Name > strDate < (Now() + dRange))

Next
End With
End Sub

It's pretty basic, but I've scowered the net looking for how to make a "greater than but less than" operator and haven't had much luck. :help

If anyone could help it would be greatly appreciated.

Thanks

Phel

JKwan
10-18-2012, 07:40 AM
Your comparison condition is formed incorrectly, try this:
pvtItem.Visible = (pvtItem.Name > strDate And pvtItem.Name < (Now() + dRange))

Phelony
10-18-2012, 08:09 AM
Thanks for that JKwan.

I am getting a type mismatch error which I think is in part due to formatting or a naming irregularity.

This should be a simple fix, but has turned into a bit of an ordeal!

P