vba “如果没有结束则阻止”错误VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25017905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:00:59  来源:igfitidea点击:

"Block if without end if" error VBA

excelvba

提问by one14four

I tried looking through the other answers, but nothing really seemed to help me out. As the title states, I'm getting a "Block if without end if" error. I'm trying to put in a conditional statement that ends the sub if its met. To be more specific, I'm formatting data that can only be formatted one job at a time. I want to automatically end the sub if it determines there are multiple jobs in the spreadsheet. Here's what I've got so far.

我尝试查看其他答案,但似乎没有任何帮助。正如标题所述,我收到“如果没有结束则阻止”错误。我试图放入一个条件语句,如果它满足则结束子程序。更具体地说,我正在格式化一次只能格式化一项作业的数据。如果它确定电子表格中有多个作业,我想自动结束子程序。这是我到目前为止所得到的。

Sub SUBNAMEHERE

(Lots of other code)

JobNo = (code that figures out how many jobs there are)
If JobNo > 1 Then
    MsgBox (warning message)
    End Sub
End If

(The rest of the code)

If anyone could help me out, it would be much appreciated.

如果有人可以帮助我,将不胜感激。

回答by cHao

Try Exit Subrather than End Sub.

尝试Exit Sub而不是End Sub

When you say End Sub, you're telling VB that you're done defining the routine. So if you haven't ended the Ifbefore that, it'll be considered incomplete.

当您说 时End Sub,您是在告诉 VB 您已经完成了对例程的定义。因此,如果您在此If之前还没有结束,它将被认为是不完整的。

Course, even if you didend the Ifbefore that, you'd almost certainly wind up getting errors about code outside of a function. (I don't know VBA all that well..but that's how most flavors of VB work.)

当然,即使您确实在此If之前结束了,您几乎肯定会在函数之外遇到有关代码的错误。(我不太了解 VBA……但这就是大多数 VB 风格的工作方式。)

回答by polarysekt

Sub SUBNAMEHERE

(Lots of other code)

JobNo = (code that figures out how many jobs there are)
If JobNo > 1 Then
    MsgBox (warning message)
    -------> End Sub  <------ 
End If

(The rest of the code)

The End Sub should go on the bottom of the Sub

End Sub 应该放在 Sub 的底部



And as your the commentors pointed out, it's Exit Subthat you're looking for.

正如您的评论者指出的那样,这Exit Sub就是您正在寻找的。