2013-05-30 55 views
0

我有这个问题,如果ElseIf块,我不知道错误在哪里。如果否则块错误

我得到的消息是“Else without if”。

我是非常新的使用如果elseif任何帮助或指导将不胜感激。

行错误的:

ElseIf booSM = True And _ 
     booInv = True And _ 
     booHOP = False _ 
     Then strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1) 

全码:

Dim txtScan As String 
Dim strTo As String 
Dim booSM As Boolean 
Dim booInv As Boolean 
Dim booHOP As Boolean 

Me.Refresh 
txtScan = Me.txtScanLocation 
booSM = booComDocSMSent 
booInv = booComDocInvSent 
booHOP = booComDocHOPSent 

If booSM = True And _ 
    booInv = True And _ 
    booHOP = True _ 
    Then strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1) & ";" & Me.txtHop.Column(1) 
ElseIf booSM = True And _ 
     booInv = True And _ 
     booHOP = False _ 
     Then strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1) 
ElseIf booSM = True And _ 
     booInv = False And _ 
     booHOP = True _ 
     Then strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtHop.Column(1) 
ElseIf booSM = False And _ 
     booInv = True And _ 
     booHOP = True _ 
     Then strTo = Me.txtInvestigator.Column(1) & ";" & Me.txtHop.Column(1) 
ElseIf booSM = True And _ 
     booInv = False And _ 
     booHOP = False _ 
     Then strTo = Me.txtServiceManager.Column(1) 
ElseIf booSM = False And _ 
     booInv = True And _ 
     booHOP = False _ 
     Then strTo = Me.txtInvestigator.Column(1) 
ElseIf booSM = False And _ 
     booInv = False And _ 
     booHOP = True _ 
     Then strTo = Me.txtHop.Column(1) 
End If 
+0

是否错误消息告诉你哪一行的错误是吗? – RichieHindle

+0

在出现的位置添加了一行。 – Dan

回答

2

查找到的If statement代码段以下逻辑:

Sub Logic() 
'Attempt 1st 
    If a = 1 And _ 
     b = 1 _ 
     Then C = 1 'end of if statement 

'Attempt 2nd 
    If a = 1 And _ 
     b = 1 _ 
     Then _ 
     C = 1 'end of if statement 

'Attempt 3rd 
    If a = 1 And _ 
     b = 1 _ 
     Then 
     C = 1 'and continuation below 
    ElseIf a = 1 And _ 
     b = 1 _ 
     Then 
     'something here 
    End If 
End Sub 

有写if conditions check三种不同的逻辑。在你的情况下,你错过了一些_ underline marks或者你应该移动一些行到下一行(关键词Then之后的代码)。

+1

谢谢你做了工作,但为什么它的行被移动到Then语句之后呢? – Dan

+0

这些只是我们都需要遵守的'if语句'的语法规则。有关更多信息,请尝试检查[此MSDN信息](http://msdn.microsoft.com/zh-cn/library/752y8abs.aspx) –

1

我没有VBA出手,但它看起来像它的一个问题,您的行结束

格式

if condition then 
    statement 
elesif conditon 
    statement 
end if 

您正在使用续行(_)和你的格式看起来像

if condition then statement 
elseif condition then statement 
endif 

所以

If booSM = True And booInv = True And booHOP = True Then 
    strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1) & ";" & Me.txtHop.Column(1) 
ElseIf booSM = True And booInv = True And booHOP = False Then 
    strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1) 
End If 

可能更好地为您 或者,如果你想保留一些多行的格式

If booSM = True And _ 
    booInv = True And _ 
    booHOP = True Then 
     strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1) & ";" & Me.txtHop.Column(1) 
ElseIf booSM = True And _ 
     booInv = True And _ 
     booHOP = False _ 
Then 
     strTo = Me.txtServiceManager.Column(1) & ";" & Me.txtInvestigator.Column(1) 
End If