调整枢轴表的过滤器

发布于 2025-02-01 08:05:29 字数 630 浏览 4 评论 0原文

我正在尝试根据预定的设置调整枢轴表的过滤器。

我想清除所有枢轴表滤波器,然后选择过滤器。

代码给了我

运行时错误'1004':无法设置Pivotfield类的PivotItems属性。

它也无法清除枢轴表上的过滤器。

Sub PC()

'code for WHS1
Worksheets("Multi_WHS_Pivot").Activate
ActiveSheet.PivotTables("Table").PivotFields("Branch").CurrentPage = "(All)"
With ActiveSheet.PivotTables("Table").PivotFields("Branch")
    .ClearAllFilters
    .EnableMultiplePageItems = True
    .PivotItems("015") = True
    .PivotItems("716") = True
    .PivotItems("710") = True
End With
Worksheets("Main Data").Activate
End Sub

I am trying to adjust the filters of a pivot table based on predetermined settings.

I want to clear all the pivot table filters then select the filters.

The code gives me

Run-time error '1004': Unable to set the PivotItems property of the PivotField class.

It also doesn't clear the filter on the Pivot Table.

Sub PC()

'code for WHS1
Worksheets("Multi_WHS_Pivot").Activate
ActiveSheet.PivotTables("Table").PivotFields("Branch").CurrentPage = "(All)"
With ActiveSheet.PivotTables("Table").PivotFields("Branch")
    .ClearAllFilters
    .EnableMultiplePageItems = True
    .PivotItems("015") = True
    .PivotItems("716") = True
    .PivotItems("710") = True
End With
Worksheets("Main Data").Activate
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你另情深 2025-02-08 08:05:29

我尚未测试它,但是我修改了您的宏,以便它通过每个枢轴项目循环。对于每个枢轴项目,如果名称不等于“ 015”,“ 716”或“ 710”,则可见属性设置为false。

Sub PC()
    
    Dim currentPivotItem As PivotItem

    'code for WHS1
    With Worksheets("Multi_WHS_Pivot").PivotTables("Table").PivotFields("Branch")
        .ClearAllFilters
        .EnableMultiplePageItems = True
        For Each currentPivotItem In .PivotItems
            If IsError(Application.Match(currentPivotItem.Name, Array("015", "716", "710"), 0)) Then
                currentPivotItem.Visible = False
            End If
        Next currentPivotItem
    End With
    
    Worksheets("Main Data").Activate
    
End Sub

I haven't tested it, but I have amended your macro so that it loops through each pivot item. For each pivot item, if the name doesn't equal "015", "716", or "710", the visible property is set to False.

Sub PC()
    
    Dim currentPivotItem As PivotItem

    'code for WHS1
    With Worksheets("Multi_WHS_Pivot").PivotTables("Table").PivotFields("Branch")
        .ClearAllFilters
        .EnableMultiplePageItems = True
        For Each currentPivotItem In .PivotItems
            If IsError(Application.Match(currentPivotItem.Name, Array("015", "716", "710"), 0)) Then
                currentPivotItem.Visible = False
            End If
        Next currentPivotItem
    End With
    
    Worksheets("Main Data").Activate
    
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文