如何使用 Excel VBA 过滤具有日期范围的数据透视项
How to filter pivot items with date range with Excel VBA
我需要帮助过滤具有日期范围的数据透视表项。这些项目的日期格式为 YYYY-MM-DD,介于 2014 年和 2018 年之间。我希望只有过去 12 个月的项目在数据透视 table 中可见。
我想出的代码首先检查枢轴下拉列表中的所有项目table。然后它应该取消选中所有不在 12 个月范围内的项目。
问题:代码没有过滤任何东西,因此所有项目仍然可见。
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
编辑*****************
当代码通过 For Each 循环时,我使用 watch window 查看变量的值和类型。 pivot.visible = true/false 方法似乎存在类型不匹配问题。任何想法可能是什么问题?
Watch Window
Snippet of the data
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
我找到了显示过去 12 个月所有项目的解决方案。我使用了日期的 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
此解决方案非常适合枢轴 table 的应用。我本身并没有解决问题,但我得到了我需要的东西。
感谢那些花时间阅读 post 并尝试提供帮助的人。
祝你有愉快的一天!
我需要帮助过滤具有日期范围的数据透视表项。这些项目的日期格式为 YYYY-MM-DD,介于 2014 年和 2018 年之间。我希望只有过去 12 个月的项目在数据透视 table 中可见。
我想出的代码首先检查枢轴下拉列表中的所有项目table。然后它应该取消选中所有不在 12 个月范围内的项目。
问题:代码没有过滤任何东西,因此所有项目仍然可见。
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
编辑***************** 当代码通过 For Each 循环时,我使用 watch window 查看变量的值和类型。 pivot.visible = true/false 方法似乎存在类型不匹配问题。任何想法可能是什么问题? Watch Window
Snippet of the data
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
我找到了显示过去 12 个月所有项目的解决方案。我使用了日期的 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
此解决方案非常适合枢轴 table 的应用。我本身并没有解决问题,但我得到了我需要的东西。
感谢那些花时间阅读 post 并尝试提供帮助的人。
祝你有愉快的一天!