首页 文章

VBA在代码执行期间更改错误处理方法

提问于
浏览
0

我有一个宏,部分代码涉及:1)检测列是否包含空单元格 - 如果是这样填充它们有一些值2)检测列是否包含包含错误的单元格(如N / A)以及是否所以填补它们的 Value

现在,如果列中没有错误/空单元格,则找到它们的行会显示“运行时错误1004,找不到单元格” .

我使用错误处理来跳过GoTo .

下面是代码 - 第一个错误处理GoTo完美地工作,而第二个错误处理Gomer预设错误,虽然有错误处理GoTo设置,似乎不起作用 . 带注释的代码:

On Error GoTo EErrorOne


'depending on file I get, below line will generate error and code successfully skips to ErrorOne label

Workbooks(nazwawb).Sheets(szitnr).Columns(ktorepole).SpecialCells (xlCellTypeBlanks)

' code to be skipped

    Workbooks(nazwawb).Sheets(szitnr).Columns(ktorepole).Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.Value = "Some Value"

' end of code to be skipped

EErrorOne:

' successfully skipped error line by now. Below line should set a new Error handling procedure.

On Error GoTo EErrorTwo



Workbooks(nazwawb).Sheets(szitnr).Columns(ktorepole).Select

' Below line generates an error but does not skip to EErrorTwo label as detailed in the current Error handling procedure

Selection.SpecialCells(xlCellTypeFormulas, 16).Select   

' code to be skipped


Selection.SpecialCells(xlCellTypeFormulas, 16).Select
    Selection.Value = "Some Value"

' end of code to be skipped


EErrorTwo:



' Below line should reset error handling procedure to normal for subsequent handling of other errors:
On Error GoTo 0

似乎忽略了错误处理过程(GoTo特定标签),而是显示错误消息,好像错误处理被重置为GoTo 0.如何跳过第二个错误?

2 回答

  • 6

    在内部错误处理例程中,似乎定义新的错误处理例程将不起作用,除非您清除以前设置的错误例程(https://excelmacromastery.com/vba-error-handling/):

    '...
    EErrorOne:
    On Error GoTo -1 'Clears error trap flag
    On Error GoTo EErrorTwo 'Reset error handling
    '...
    

    Edit after accepted:
    正如评论中所讨论的那样, On Error GoTo -1 清除错误陷阱标志,而 Err.Clear 仅清除错误 .
    下面的代码通过创建两个错误并尝试捕获它们来说明这一点 .

    On Error Goto -1 允许 On Error GoTo NextLabel 行捕获第二个错误,并在发生错误时代码跳转到标签 .
    Err.Clear 保持第一个错误,所以当第二个错误发生时,将显示错误消息而不是跳转到标签的代码 .

    Sub ClearErr()
    
        Dim x As Long
        Dim y As Worksheet
    
        'Jump to label when "Division by 0" occurs.
        On Error GoTo ErrorLabel
    
        x = 1 / 0
        Debug.Print x
    
    ErrorLabel:
    
        'On Error GoTo -1 'Next error is trapped.
        Err.Clear   'Untrapped error on y=Worksheets(1)
    
        'Jump to label when "Object Variable not set" occurs.
        On Error GoTo NextLabel
    
        y = Worksheets(1)
        Debug.Print y.Name
    
    NextLabel:
    
    End Sub
    
  • 2

    你不会在它们发生时清除你的错误,只是试图跳过它们并且代码想知道错误发生了什么 .

    正如Chip Pearson在他的网站上所说:

    当引发第一个错误时,执行转移到Err1:之后的行 . 发生第二个错误时,错误处理程序仍处于活动状态,因此On Error语句不会捕获第二个错误

    并继续

    Resume语句指示VBA在代码中的指定点继续执行 . 您只能在错误处理块中使用Resume;任何其他用途都会导致错误 . 此外,除了退出过程之外,Resume是唯一的方法,以摆脱错误处理块 . 不要使用Goto语句将代码执行从错误处理块中引出 . 这样做会导致错误处理程序出现奇怪问题 . http://www.cpearson.com/excel/errorhandling.htm

    理想的方法是首先避免错误 - 在打开它之前检查工作簿是否存在,在尝试引用它之前检查工作簿中是否存在工作表,如果发生错误则跳出例程的主体,处理错误然后再次跳回来 .

    举个例子:

    Sub Test()
    
        On Error GoTo ERR_HANDLE
    
        '.... code ....
    
    FAST_EXIT:
        'Code to tidy up, clear references etc before finishing.
    
    Exit Sub
    
    ERR_HANDLE:
        Select Case Err.Number
            Case 1004
                'Code to resolve error
                '.
                '.
                'Resume - clear error, jump back to line that caused it.
                'Resume Next - clear error, jump to line after the one that caused error.
                'Resume FAST_EXIT - clear error and go to line label.
    
            Case 92 'Do something else to resolve error.
    
            Case Else
        End Select
    
    End Sub
    

相关问题