【问题标题】:error message not appearing though there is no error and put exit sub before error hadeler statment in vba尽管没有错误并没有出现错误消息并将退出子放在 vba 中的错误 hadeler 语句之前
【发布时间】:2023-03-15 19:15:01
【问题描述】:

我一直在想如果有错误如何弹出消息框。我有下面的代码,但是如果有错误,这段代码不会跳转到错误处理程序并退出子。

Sub Validate_Region()

Dim str_DEPARTMENT_NAME As String

On Error GoTo Reion_Error

    If Left(Range("M4"), 2) = "As" Then
    str_DEPARTMENT_NAME = "India"
    ElseIf Left(Range("M4"), 2) = "ME" Then
    str_DEPARTMENT_NAME = "Middle East"
    End If
    'Get value of region selected in Master File
    str_regionvalue = Workbooks("Master Report").Sheets("Home").Range("T8")

    If str_regionvalue = str_DEPARTMENT_NAME Then
    MsgBox ("You have selected " & str_regionvalue & " region"), vbInformation

    End If
Exit Sub

Reion_Error:
    MsgBox ("Please select the correct Region.") & vbNewLine & vbNewLine & ("You have selected " & str_regionvalue & " region" & " In Home Sheet of Master Report and pulled the data for " & str_DEPARTMENT_NAME & " region"), vbCritical
    ActiveWorkbook.Close

End Sub

【问题讨论】:

  • 您看到错误了吗?在哪一行?
  • 嗨蒂姆,没有任何错误。如果有错误,它只会触发代码,但不会跳转到错误处理程序消息。
  • 在 Vb 编辑器的工具 >> 选项下检查您的错误设置。确保它没有设置为“中断所有错误”

标签: excel error-handling vba


【解决方案1】:

我只是复制/粘贴了您的代码,它对我很有效:

我不知道这个 sub 是从哪里调用的,str_regionvalue 上使用了什么类型的变量(好像是 Variant)

那么你可以用这个做什么:

  1. 在第 19 行删除 Exit Sub
  2. 将 Err.Number 检查放入错误处理程序中:

Reion_Error: If Err.Number <> 0 Then MsgBox ("Please select the correct Region.") & vbNewLine & vbNewLine & ("You have selected " & str_regionvalue & " region" & " In Home Sheet of Master Report and pulled the data for " & str_DEPARTMENT_NAME & " region"), vbCritical 'by the way, you can try to set ActiveWorkbook.Saved to True, to get rid from pop-ups on Close ActiveWorkbook.Close End If

  1. 尝试通过调试查找错误号(debug.print err.number 或放置一些stop 手动执行此操作)。或者将 err.number 添加到 Watches 并检查 Break when value changes,以便跟踪错误出现和清除的位置(如果出现)。

Err object

附:你有一些可笑的 MsgBoxes,如果你真的喜欢括号,可以这样使用:

    Call MsgBox("Please select the correct Region." & vbNewLine & vbNewLine  & _           
            "You have selected " & str_regionvalue & " region" & " In Home Sheet of Master Report and pulled the data for " & str_DEPARTMENT_NAME & " region", vbCritical)

【讨论】:

  • 嗨,你能把你的代码发给我吗?我尝试了上述选项,但仍然无法正常工作。如果我跳过代码中的“退出子”行,那么如果完全有错误,代码就会执行消息框。如果没有错误,那么代码仍然会抛出应该跳过的错误消息框。在其他情况下,如果我输入“Exit Sub”,如果有错误,代码不会跳转到错误处理程序,并且只运行到“Exit sub”。
  • 我只是将您的代码放入新的工作簿并将其绑定到“Worksheet_Change”事件,因为没有名为“Home”的工作表,也没有名为“Master Report”的工作簿,我得到了 100% 的错误。跨度>
  • 我不知道你得到了什么“错误”MsgBox,当 err.number 不等于 0 时从错误处理程序中引发的那个?不要忘记用“on error goto 0”关闭你的“on error”语句!
  • 只是为了澄清我在这里指的“错误消息框”是​​一个消息框,当出现错误时应该出现。根据我的代码,虽然没有错误,但出现错误消息。如果我跳过添加“退出子”。如果我添加代码停止到“Exit Sub”行并且尽管有错误但不要跳转到错误处理程序。
  • 看起来你想要在 str_regionvalue str_DEPARTMENT_NAME 时出错,对吗?因为你寻找正确的区域,当 str_regionvalue str_DEPARTMENT_NAME 很明显时,没有错误,所以你需要自己用 err.raise if str_regionvalue str_DEPARTMENT_NAME 提出一个
【解决方案2】:

在 VB 编辑器中的工具 >> 选项下:在常规选项卡上,确保您没有将“错误捕获”设置为“中断所有错误”

【讨论】:

    【解决方案3】:

    检查您的语法,您的 msgbox 语句中有很多不必要的括号和逗号。尝试类似:

    returnval = MsgBox ("Put message here" , vbInformation)
    

    MsgBox "another message " & vbnewline & "some more message", vbcritical
    

    代码看起来很好并且现在可以编译,但我认为您的错误可能在其他地方。可能在:

    str_regionvalue = Workbooks("Master report").Sheets("Home").Range("T8") 
    

    我建议:

    1. 留下'exit sub'。没错。
    2. 将“Option Explicit”添加到代码顶部 - 它会强制您声明所有变量(未声明 str_regionvalue)

    3. 在 VB 编辑器中将您的“工具”>>“选项”设置为“中断未处理的错误”。

    4. 在代码顶部放置一个断点(Debug >> Add Watch)并使用Debug >> Step Into逐步运行。

    【讨论】:

    • 非常感谢@LeasMaps 的回复。我尝试了上面的代码,但它不起作用。如果我跳过代码中的“退出子”行,那么如果完全有错误,代码就会执行消息框。如果没有错误,那么代码仍然会抛出应该跳过的错误消息框。
    • 嗨 Swapnil - 代码看起来很好并且现在可以编译,但我认为您的错误可能在其他地方。可能在:str_regionvalue = Workbooks("Master Report").Sheets("Home").Range("T8") ??我建议: 1. 保留“退出子”。这是正确的。 2. 在代码顶部添加'Option Explicit' - 它会强制你声明所有变量(str_regionvalue 没有声明) 3. 在代码顶部放置一个断点并使用
    猜你喜欢
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2016-02-17
    相关资源
    最近更新 更多