【问题标题】:Using On Error GoTo with If, elseIf statements, msg repeats itself将 On Error GoTo 与 If、elseIf 语句一起使用,msg 会重复自身
【发布时间】:2020-10-01 17:11:03
【问题描述】:

我是 On Error GoTo 的新手。我有一个检查客户状态的功能。如果为0,则通过,否则代码应为5-18。我正在尝试到达错误到达的位置,弹出一个带有该错误的消息框,一旦用户单击确定,我的应用程序将从头开始并运行以查看该错误是否已修复或是否存在另一个在那之后的错误。不确定 On Error GoTo 是否是最好的方法。我不知道如何根据返回的数字而不是一条通用消息来显示每条单独的消息。目前,当我运行时,我一直不得不按确定,然后我收到错误消息并按确定等。

On Error GoTo Error
    Debug.Print "Starting Check."
TryAgain:
        client.Read "State", State
    If State <> 0 Then
    ElseIf code = 5 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 6 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 7 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 8 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 9 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 10 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 11 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 12 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 13 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 14 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 15 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 16 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 17 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 18 Then
        Message = "*** - ERROR: Description Here"
    End If
    Else
MsgBox ("Successful")
    Exit Function
    End If
    
Error:
 'Display Message from above is what i am having trouble with
  Resume TryAgain

【问题讨论】:

  • Exit Function移出End If并放在Error:之前
  • 另外从你的代码看来你有一个额外的End If?

标签: excel vba onerror


【解决方案1】:

这是一个建议:

不要在 VBA 中使用 GoTo 作为控制程序流程的一种方式,除了“on error goto”的错误和退出处理

它将引导您进入意大利面条代码

我用一些亮点重构了你的代码:

  • 不要重复自己的原则
  • 通过单独的函数处理错误消息
  • 用循环处理读取客户端的成功

我对变量类型和其他东西做了一些假设,所以请阅读代码的 cmets 并对其进行自定义以满足您的需求。

附言。不要忘记添加退出和错误处理

希望这能让您走上正确的道路。如果有帮助,请告诉我。

代码

Public Function Test()

    ''''''Your code here'''''

    Debug.Print "Starting Check."
    
    Dim retry As Boolean

    ' Try to not repeat code / text if you can
    Dim messagePrefix As String
    messagePrefix = "*** - ERROR: "
    
    Do
        
        ' Read and assign state
        client.Read "State", State
        
        If State = 0 Then
            retry = False
            MsgBox ("Successful")
            Exit Do
        Else
            ' State <> 0 Add error description
            message = GetClientMessage(code)
            'Display message from above
            MsgBox messagePrefix & message
            retry = False
        End If
        
    Loop While retry = False
    
    ' Do something else???
    
' Add the exit and error handling labels

End Function


Private Function GetClientMessage(ByVal code As Integer) As String
    
    Dim message As String
    
    Select Case code
    Case 5
        message = "Description Here"
    Case 6
        message = "Description Here"
    Case 7
        message = "Description Here"
    Case 8
        message = "Description Here"
    Case Else
        message = "Description Here"
    End Select
    
    GetClientMessage = message

End Function

【讨论】:

  • 感谢您的结构和指针,在玩弄了它之后,终于让它工作了!好老师。
猜你喜欢
  • 2017-05-09
  • 2011-06-09
  • 1970-01-01
  • 2017-03-02
  • 2013-01-26
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
相关资源
最近更新 更多