【问题标题】:How to filter pivot items with date range with Excel VBA如何使用 Excel VBA 过滤具有日期范围的数据透视项目
【发布时间】:2018-09-20 18:31:06
【问题描述】:

我需要帮助过滤具有日期范围的数据透视项目。这些项目是 2014 年至 2018 年间格式为 YYYY-MM-DD 的日期。我希望只有过去 12 个月的项目在数据透视表中可见。

我想出的代码首先检查数据透视表下拉列表中的所有项目。然后它应该取消选中所有不在 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 循环时,我使用监视窗口查看变量的值和类型。看来我的 pivot.visible = true/false 方法存在类型不匹配问题。任何想法可能是什么问题? Watch Window

Snippet of the data

【问题讨论】:

  • 你有没有可能有类似日期的文本?
  • 不,数据透视项目采用日期格式。 Year(date) 和 Year(pivot) 返回一个整数。
  • Year 仍然适用于看起来像日期的文本。
  • 数据透视表从另一个工作簿获取其源数据。我已经验证了源数据中单元格的格式是日期格式 YYYY-MM-DD。
  • 可以添加数据的sn-p截图吗?

标签: excel vba filter ms-office pivot-table


【解决方案1】:

我找到了显示过去 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

此解决方案非常适合数据透视表的应用。我本身并没有解决问题,但我得到了我需要的东西。

感谢那些花时间阅读帖子并尝试提供帮助的人。

祝你有美好的一天!

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

    • 您能解释一下如何在上面的代码中实现这一点吗?我应该摆脱 For Each 循环吗?我是 vba 初学者。
    • 就用这5行代码试试看行不行?
    • 使用日期过滤器,Between 是另一种获得与选择所需日期相同结果的方法。您可以手动使用这两种方法来查看是否得到相同的结果?如果相同,您可以让我的编码过滤日期之间(使用过滤日期之间看不到选择了哪些日期但结果相同)。
    • 我在上面的照片中解释。看懂了吗?
    • 由于某种原因我无法手动添加过滤器,Excel 不允许。
    猜你喜欢
    • 2015-06-30
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多