vba “Block If without End If”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21570770/
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
"Block If without End If" error
提问by user3273965
I'm getting a compile error for this code:
我收到此代码的编译错误:
Public Sub CommandButton1_Click()
If TextBox1.Text = "log off" Then
Shell "cmd.exe /c shutdown -l", vbHide: TextBox2.Text = "Logging off"
If TextBox1.Text = "shutdown" Then
Shell "cmd.exe /c shutdown -s", vbHide: TextBox2.Text = "Shutting Down"
If TextBox1.Text = "restart" Then
Shell "cmd.exe /c shutdown -r", vbHide: TextBox2.Text = "Restarting"
Else
MsgBox "Command Not Defined",vbCritical
End Sub
Now it comes up with this Error Message of "Block If without End If". Why?
现在它出现了“Block If without End If”的错误消息。为什么?
回答by Dmitry Pavliv
You have missed End If
:
你错过了End If
:
Public Sub CommandButton1_Click()
If TextBox1.Text = "log off" Then
Shell "cmd.exe /c shutdown -l", vbHide: TextBox2.Text = "Logging off"
ElseIf TextBox1.Text = "shutdown" Then
Shell "cmd.exe /c shutdown -s", vbHide: TextBox2.Text = "Shutting Down"
ElseIf TextBox1.Text = "restart" Then
Shell "cmd.exe /c shutdown -r", vbHide: TextBox2.Text = "Restarting"
Else
MsgBox "Command Not Defined", vbCritical
End If
End Sub
Actually in your code you will always have TextBox2.Text
equals to "Restarting"
. That's why you should use ElseIf
statement.
实际上,在您的代码中,您将始终拥有TextBox2.Text
等于"Restarting"
. 这就是为什么你应该使用ElseIf
语句。
You could use Select Case
statement as well:
您也可以使用Select Case
语句:
Public Sub CommandButton1_Click()
Select Case TextBox1.Text
Case "log off"
Shell "cmd.exe /c shutdown -l", vbHide: TextBox2.Text = "Logging off"
Case "shutdown"
Shell "cmd.exe /c shutdown -s", vbHide: TextBox2.Text = "Shutting Down"
Case "restart"
Shell "cmd.exe /c shutdown -r", vbHide: TextBox2.Text = "Restarting"
Case Else
MsgBox "Command Not Defined", vbCritical
End Select
End Sub