-1

我遇到了这个 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
4

2 回答 2

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关键字后面的代码)。

于 2013-05-30T08:41:33.073 回答
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
于 2013-05-30T08:48:14.893 回答