简体   繁体   English

如何使用Excel VBA筛选具有日期范围的数据透视项

[英]How to filter pivot items with date range with Excel VBA

I need help filtering pivot items with a date range. 我需要帮助来过滤具有日期范围的数据透视项目。 The items are dates in the format YYYY-MM-DD between 2014 and 2018. I would like for only the items of the past 12 months to be visible in the pivot table. 这些项目的日期为2014年至2018年之间,格式为YYYY-MM-DD。我希望过去12个月的项目在数据透视表中可见。

The code I came up with first checks all the items in the drop-down list of the pivot table. 我首先想到的代码将检查数据透视表的下拉列表中的所有项目。 Then it should uncheck all items that are not within the 12 months range. 然后,应取消选中不在12个月范围内的所有项目。

Problem: the code does not filter anything, therefore all items are still visible. 问题:代码不过滤任何内容,因此所有项目仍然可见。

Dim pivot As PivotItem
Dim currentMonth As Integer
Dim currentYear As Integer
currentMonth = Month(Date)
currentYear = Year(Date)

ActiveSheet.PivotTables("OEMI").RefreshTable
ActiveSheet.PivotTables("OEMI").PivotFields("Date sent to Coordinator").EnableMultiplePageItems = True

For Each pivot In ActiveSheet.PivotTables("OEMI").PivotFields("Date sent to Coordinator").PivotItems
        If Not (Year(pivot) = currentYear And Month(pivot) <= currentMonth) Or _
                (Year(pivot) = currentYear - 1 And Month(pivot) > currentMonth) Then
                    pivot.Visible = False

                Else
                'Do nothing and stay visible in the drop-down list
                End If
    Next pivot

EDIT***************** I used the watch window to see the value and type of the variables as the code goes through the For Each loop. 编辑******************我使用观察窗口在代码遍历For Each循环时查看变量的值和类型。 It seems that I have a type mismatch issue with the pivot.visible = true/false method. 看来我的pivot.visible = true / false方法存在类型不匹配的问题。 Any ideas what could be the problem? 任何想法可能是什么问题? Watch Window 观察窗

Snippet of the data 数据片段

I found a solution to display all items of the last 12 months. 我找到了一种解决方案,可以显示过去12个月的所有项目。 I've used the excel numerical value of dates and used this to filter the pivot items. 我使用了日期的Excel数值,并用它来过滤数据透视表项。

'Define the pivot items
Dim pivot As PivotItem

'Refresh the pivot table
ActiveSheet.PivotTables("OEMI").RefreshTable 'VBA will now use the current pivot item rather than the previously cached _
                                              pivot items


'Add filters for "Date sent to Coordinator"
ActiveSheet.PivotTables("OEMI").PivotFields("Date sent to Coordinator").EnableMultiplePageItems = True 'Select Multiple Items

'Define the date variables
Dim CurrentDate As Long 'Convert the current date to excel numerical date
Dim LastYear As Long
LastYear = CurrentDate - 365 'Numerical value of the date 12 months ago

For Each pivot In ActiveSheet.PivotTables("OEMI").PivotFields("Date sent to Coordinator").PivotItems

    If CLng(pivot) >= LastYear Then
        pivot.Visible = True
    Else
        pivot.Visible = False
    End If

Next pivot

This solution works perfectly for the application of the pivot table. 该解决方案非常适合数据透视表的应用。 I didn't solve the problem per se, but I get what I need. 我本身并没有解决问题,但是我得到了我所需要的。

Thanks to those who took the time to read the post and try to help. 感谢那些花时间阅读帖子并尝试提供帮助的人。

Have a great day! 祝你有美好的一天!

DateFrom = DateAdd("yyyy", -1, Date)
DateTo = Date

ActiveSheet.PivotTables("OEMI").PivotCache.Refresh
ActiveSheet.PivotTables("OEMI").PivotFields("Date Sent to Coordinator").ClearAllFilters
ActiveSheet.PivotTables("OEMI").PivotFields("Date Sent to Coordinator").PivotFilters.Add Type:=xlDateBetween, Value1:=DateFrom, Value2:=DateTo

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM