设置 VBA pivotfields.currentpage 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21587688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:46:56  来源:igfitidea点击:

Error when setting VBA pivotfields.currentpage

excelvbaexcel-vba

提问by Chandler R Reeves

I am trying to make a macro that will automaticly adjust the filters for several pivot tables based on user input, however if the user puts in something that isn't avaialble the code produces an error when trying to apply the filter.

我正在尝试制作一个宏,该宏将根据用户输入自动调整多个数据透视表的过滤器,但是如果用户输入了不可用的内容,则代码在尝试应用过滤器时会产生错误。

Is there anyway to check which filters are available to select?

无论如何要检查可以选择哪些过滤器?

Example: One pivot table has three filters available (Year, Month, Type [Complaint, Praise, Both]) but if during a month there weren't any complaints then there is an error.

示例:一个数据透视表具有三个可用过滤器(年、月、类型 [投诉、表扬、两者]),但如果在一个月内没有任何投诉,则存在错误。

Code:

代码:

With PTable
    .PivotFields("Year").CurrentPage = Y
    .PivotFields("Month").CurrentPage = M
    .PivotFields("Type").CurrentPage = T 'Error line if T isn't valid
End With

回答by Siddharth Rout

Further to my comments, try this

除了我的评论,试试这个

With PTable
    .PivotFields("Year").CurrentPage = Y
    .PivotFields("Month").CurrentPage = M
    On Error Resume Next
    .PivotFields("Type").CurrentPage = T 'Error line if T isn't valid
    If Err.Number <> 0  Then
        Msgbox "Filter Didn't get applied"
    End If
    On Error GoTo 0
End With