0
我正在编写一个宏,以根据 年份过滤器更改我的数据透视表中的数据。只要数据透视表中的数据在以下字符串格式的2年 - 月 之内,就应该显示。使用VBA按日期间隔过滤数据透视表
2016 - 01
2016 - 05
日期间隔也是这种格式。 这根本没有更新数据透视表。我感谢任何反馈或替代更新数据透视表给定两个日期间隔作为其过滤器。
私人小组Worksheet_Change(BYVAL目标作为范围)
Dim shPT As Worksheet
Dim pt As PivotTable
Dim pi As PivotItem
Dim d As Date
Dim dStart As Date
Dim dEnd As Date
'' G4 and G7 are the cell locations of the start and ending year-months.
dStart = DateSerial(Left(Range("G4"), 4), Right(Range("G4"), 2), 15)
dEnd = DateSerial(Left(Range("G7"), 4), Right(Range("G7"), 2), 15)
'' Calendar Filter in the Pivot Tables
Dim strCal
strCal = "Date Entered UTC"
On Error Resume Next
Application.EnableEvents = False
Application.ScreenUpdating = False
Set shPT = ActiveWorkbook.Worksheets("PivotTables")
For Each pt In shPT.PivotTables
With pt.PageFields(strCal)
For Each pi In .PivotItems
'' Loop through all year-month for each pi.
For d = dStart To dEnd Step 30
If pi.Value = Year(d) & " - " & Month(d) Then
.CurrentPage = Year(d) & " - " & Month(d)
Exit For
Else
.CurrentPage = "(All)"
End If
Next d
Next pi
End With
Next pt
Application.EnableEvents = True
Application.ScreenUpdating = True
结束子