I have few pivotfields and I would like to mark the pivotitems of the fields with some conditions. I have tried using .visible and also have used this application.screenupdating and application.displayfilters. But ist not selecting or deseleting the pivotitems. I would really appreciate each reply.
Filter_PivotField_by_Date_Range. This is a function I am calling in my main just to give my field Name and the Dates I want to compare.
After the 2 if conditions I want it to mark (select/Tickmark) the pivotitems in the field and otherwise unmark/ not select the other ones. Make the other one's invisible and then just Exit Sub.
If you guys have any questions I would really help it to make you all clear. Please any comments or code will be appreciated.
Sub Filter_PivotField_by_Date_Range(pvtField As PivotField, it1 As Date, it2 As Date)
Dim bTemp As Boolean, i As Long
Dim dtTemp As Date, dtTemp1 As Date
dtTemp = Format(CDate(it1), "dd/mm/yyyy")
dtTemp1 = Format(CDate(it2), "dd/mm/yyyy")
On Error Resume Next
With pvtField
For i = 1 To .PivotItems.Count
dtFrom = .PivotItems(i)
If (dtTemp <= dtFrom) Then
If (dtTemp1 >= dtFrom) Then
On Error Resume Next
Application.ScreenUpdating = False
Application.DisplayAlerts = False
.PivotItems(i).Visible = True
MsgBox (dtFrom)
Else
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Exit Sub
End If
End If
Next i
End With
End Sub
I have tried your code with a small Modification. It worked perfectly for me. Also i had this assumption, your 'it1' is Start Date and 'it2' is End date.
Storing Date range value to array.