I am new to this forum, and would really appreciate some guidance on an issue I've been dealing with for the past 4 days.
I have a macro to filter a pivot table for dates before a date on cell B5 in the same sheet. Unfortunately, it yields some data, but not all the data.
Below is the code I have (based on research/copy paste), I apologize if the code is not best practice. I have also attached a picture for reference as well. Thank you in advance for your review and support.
'Sub BeforeDate()
Dim sSheetName As String
Dim sPivotName As String
Dim sFieldName As String
Dim sFilterCrit As String
Dim pi As PivotItem
'Set the variables
sSheetName = "PivotTable"
sPivotName = "PivotTable1"
sFieldName = "apptdate"
'sFilterCrit = "5/2/2019"
sFilterCrit = ThisWorkbook.Worksheets(sSheetName).Range("B5").Value
With ThisWorkbook.Worksheets(sSheetName).PivotTables(sPivotName).PivotFields(sFieldName)
'Clear all filter of the pivotfield
.ClearAllFilters
'Loop through pivot items of the pivot field
'Hide or filter out items that do not match the criteria
For Each pi In .PivotItems
If pi > Range("B5") Then
pi.Visible = False
Else
pi.Visible = True
End If
Next pi
End With
End Sub
