【问题标题】:Error handler working even without errors错误处理程序即使没有错误也能正常工作
【发布时间】:2018-03-26 05:15:22
【问题描述】:

我对 Excel VBA 很陌生,并且在错误处理程序方面遇到了麻烦(使用 On Error Goto [label])。我的代码有点遵循这个流程:

StartDate = Format(Cells(11, 4).Value, "yyyy-mm-dd")
EndDate = Format(Cells(12, 4).Value, "yyyy-mm-dd")

For Each WS In Worksheets
    If WS.Name Like "WS_Name" Then
        Exist = True
        Exit For
    End If
Next

On Error GoTo EHandler: '[ERROR HANDLER POINT A]
If Exist = True Then
    On Error GoTo EHandler: '[ERROR HANDLER POINT B]
    ActiveWorkbook.Sheets("WS_Name").Select
    Sheets("WS_Name").Cells.ClearContents
    With 'some code that uses the StartDate and EndDate in a SQL query in a database
    End With
Else
    ActiveWorkbook.Worksheets.Add.Name = "WS_Name"
    With 'some code that uses the StartDate and EndDate in a SQL query in a database
    End With
End If
Exit Sub

EHandler:
    'some code that would show the error number and desc

End Sub

我想为用户可能输入比我的 StartDate 更早的 EndDate 的事件设置错误处理程序。我尝试在 ERROR HANDLER POINTS A & B 上插入 On Error Goto [label] 并得到不同的结果。

如果我的 On Error Goto [label] 位于 POINT A,则 EHandler 不起作用。

如果我的 On Error Goto [label] 在 POINT B 上,即使输入的日期正确,EHandler 也会工作。

我该如何处理这个?

【问题讨论】:

  • 不应该EndDate = 'some code 知道StartDate 以便提前完成检查吗?
  • If Exist = True Then 变为 If Exist ThenActiveWorkbook.Sheets("WS_Name").Select : Sheets("WS_Name").Cells.ClearContents 变为 ActiveWorkbook.Sheets("WS_Name").Cells.ClearContents(至少!)。

标签: vba excel


【解决方案1】:

您当前的代码可以简化为这样(注意您的 With 语句仍然没有引用对象)。

Option Explicit

Sub test()

    StartDate = Format(Cells(11, 4).Value, "yyyy-mm-dd")
    EndDate = Format(Cells(12, 4).Value, "yyyy-mm-dd")

    If EndDate < StartDate Then
        MsgBox "End Date is earlier than StartDate. Please select an end date after the start date."
        Exit Sub
    End If

    For Each WS In Worksheets

        If WS.Name Like "WS_Name" Then
            Exist = True
            Exit For
        End If

    Next

    If Exist Then

        ThisWorkbook.Sheets("WS_Name").Cells.ClearContents

        With                                   'some code that uses the StartDate and EndDate in a SQL query in a database

        End With

    Else

        ActiveWorkbook.Worksheets.Add.Name = "WS_Name"

        With                                    'some code that uses the StartDate and EndDate in a SQL query in a database

        End With

    End If

End Sub

【讨论】:

  • 我怀疑(但不确定)With/End 之间的代码是相同的,因此可以排除更多重复。 With 可能是 With ThisWorkbook.Sheets("WS_Name")
  • 是的.....我认为你可能是对的,我真的很恼火让它保持原样......红色警告让我发疯......很抱歉,直到我发布此内容后,您的上述评论才显示出来。页面刷新似乎有些慢。
  • 更像我的慢打字:-)
【解决方案2】:

我想为用户可能发生的事件设置错误处理程序 输入比我的 StartDate 更早的 EndDate。

If EndDate < StartDate Then
    ' Some code here
Else
    ' all your other code here
End If

您编写的代码取决于您要如何处理错误。以上为您提供了一些灵活性,但您也可以悄悄纠正用户错误的“软失败”

If EndDate < StartDate Then
    ' Swap the dates
    TempDate = EndDate
    EndDate = StartDate
    StartDate = TempDate
End If
' all your other code here

找出可能发生的潜在错误并在代码中处理它们总是一个好主意;将On Error 位留作意外。如果您可以编写代码以避免所有潜在的错误(包括像上面那样验证用户输入),那就更好了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 2021-01-22
    相关资源
    最近更新 更多