【问题标题】:Catch Error for Pivot filter and Exit Function捕获枢轴过滤器和退出函数的错误
【发布时间】:2021-06-01 19:03:38
【问题描述】:

我正在尝试将数据透视字段设置为过滤,如果出现错误(这意味着 Excel 无法找到数据),则弹出消息错误。

我尝试了On Error GoTo ErrMsg,但无论过滤器是否出错都会退出函数。

If country = "All" Then

Else
    With Sheet6.PivotTables("PivotTable1").PivotFields("Operator Country")
        .Orientation = xlPageField
        .Position = 1
    End With
    On Error GoTo ErrMsg
    Sheet6.PivotTables("PivotTable1").PivotFields("Operator Country").CurrentPage _
      = country
    Exit Function 
    
ErrMsg:
    MsgBox ("No data on " & country & "!")

End If

【问题讨论】:

    标签: excel vba error-handling pivot-table


    【解决方案1】:

    您可以暂停此特定任务的错误处理,然后通过检查 Err.Number 属性来检查是否引发了错误。

    例如:

    On Error Resume Next
    Sheet6.PivotTables("PivotTable1").PivotFields("Operator Country").CurrentPage = country
    
    'check if error occurred
    If Err.Number > 0 then
        MsgBox ("No data on " & country & "!")
    End If
    
    'resume normal error handling here
    

    【讨论】:

    • 我必须声明 Err.Number 属性吗?
    • 不,它是全局 Err 对象的内置属性。
    猜你喜欢
    • 2012-05-23
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    相关资源
    最近更新 更多