Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Excel VBA筛选日期范围为的数据透视项_Excel_Vba_Filter_Ms Office_Pivot Table - Fatal编程技术网

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

如何使用Excel VBA筛选日期范围为的数据透视项,excel,vba,filter,ms-office,pivot-table,Excel,Vba,Filter,Ms Office,Pivot Table,我需要帮助筛选具有日期范围的轴心项目。这些项目是2014年至2018年间YYYY-MM-DD格式的日期。我只希望过去12个月的项目在透视表中可见 我使用的代码首先检查透视表下拉列表中的所有项。然后,它应该取消选中所有不在12个月范围内的项目 问题:代码没有过滤任何内容,因此所有项目仍然可见 Dim pivot As PivotItem Dim currentMonth As Integer Dim currentYear As Integer currentMonth = Month(Date)

我需要帮助筛选具有日期范围的轴心项目。这些项目是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
Dim pivot作为数据透视项
将currentMonth设置为整数
将当前年份设置为整数
currentMonth=月份(日期)
当前年份=年份(日期)
ActiveSheet.PivotTables(“OEMI”).RefreshTable
ActiveSheet.PivotTables(“OEMI”)。数据透视字段(“发送给协调员的日期”)。EnableMultiplePageItems=True
对于ActiveSheet.PivotTables(“OEMI”).PivotFields(“发送给协调人的日期”).PivotItems中的每个数据透视
如果不是(年(轴)=当前年和月(轴)当前月),则
pivot.Visible=False
其他的
'不执行任何操作并在下拉列表中保持可见
如果结束
下一个支点
编辑***************** 当代码通过For-Each循环时,我使用watch窗口查看变量的值和类型。pivot.visible=true/false方法似乎存在类型不匹配问题。你知道有什么问题吗?


我找到了一个显示过去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
此解决方案非常适用于透视表的应用。我没有解决问题本身,但我得到了我需要的

感谢那些花时间阅读这篇文章并试图提供帮助的人


祝你今天愉快

您的文本可能看起来像日期吗?不,透视项是日期格式的。年(日期)和年(透视)返回一个整数。
Year
仍将使用类似于文本的日期。透视表从其他工作簿获取其源数据。我已经验证了源数据中的单元格格式是YYYY-MM-DD日期格式。能否添加数据片段的屏幕截图?能否在上面的代码中解释如何实现这一点?我应该去掉For-Each循环吗?。我是一名vba初学者。只需使用这5行代码,然后尝试查看是否确定?使用日期过滤器,中间是另一种方法,可以获得与选择所需日期相同的结果。您可以手动使用这两种方法来查看是否得到相同的结果?如果相同,您可以使用我的编码来筛选日期之间的日期(使用filter date between无法查看选择了哪些日期,但结果相同)。我在上面的照片中进行了解释。看到了吗?你能理解吗?我不能手动添加过滤器,因为某些原因,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