2013-05-30 74 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