如果Else Statement发出错误,我错过了End If语句

时间:2016-07-07 20:48:11

标签: if-statement vbscript

我希望看到另一组眼睛来查看我的代码。我正在写一篇相对较大的If ... Else ...声明。看来一切都编码正确。我已经完成了整个剧本,但有一个地方不断给我一个"Expected 'End If'"当我觉得我不需要那个。

简单地说,我想说:如果等等,请为事情做这些事。否则如果等等,只做其中一件事。

这是代码块:

If(sLogFile <> "" AND nPing <> 1) Then
    pingStatus = 1

    If(findTag(sLogFile, daRunning) = 1) Then
        daStatus = 1
    Else If(findTag(sLogFile, daNotRunning) = 1) Then
        daStatus = 2
    Else
        daStatus = 3
    End If

    If(daStatus = 1 AND findTag(sLogFile, daDataFlowing) = 1) Then
        daFlowStatus = 1
    Else If(daStatus = 1 AND findTag(sLogFile, daDataNotFlowing) = 1) Then
        daFlowStatus = 2
    Else If(daStatus = 1 AND findTag(sLogFile, daDataUnchecked) = 1) Then
        daFlowStatus = 3
    Else If(daStatus <> 1) Then
        daFlowStatus = 4
    End If

    If(findTag(sLogFile, aeRunning) = 1) Then
        aeStatus = 1
    Else If(findTag(sLogFile, aeNotRunning) = 1) Then
        aeStatus = 2
    Else
        aeStatus = 3
    End If

    If(findTag(sLogFile, aeDataUnchecked) = 1) Then
        aeFlowStatus = 2
    Else If(findTag(sLogFile, aeDataExecutionError) = 1) Then
        aeFlowStatus = 3
    Else If(findTag(sLogFile, aeDataConnectionError) = 1) Then
        aeFlowStatus = 4
    Else If(findTag(sLogFile, aeTimeStamp) = 1) Then
        location = InStr(sLogFile, aeTimeStamp)
        leftTrimmedString = LTrim(Mid(sLogFile, (location - 2)))
        location = InstrRev(leftTrimmedString, ":")
        dateString = Trim(Mid(leftTrimmedString, 1, (location + 5)))
        timeDiff = DateDiff("h", dateString, Now)
        If(timeDiff > 5) Then
            aeFlowStatus = 5
        Else
            aeFlowStatus = 1
        End If
    End If

Else If(nPing = 1) Then
    pingStatus = 2

    If(findTag(sLogFile, aeDataUnchecked) = 1) Then
        aeFlowStatus = 2
    Else If(findTag(sLogFile, aeDataExecutionError) = 1) Then
        aeFlowStatus = 3
    Else If(findTag(sLogFile, aeDataConnectionError) = 1) Then
        aeFlowStatus = 4
    Else If(findTag(sLogFile, aeTimeStamp) = 1) Then
        location = InStr(sLogFile, aeTimeStamp)
        leftTrimmedString = LTrim(Mid(sLogFile, (location - 2)))
        location = InstrRev(leftTrimmedString, ":")
        dateString = Trim(Mid(leftTrimmedString, 1, (location + 5)))
        timeDiff = DateDiff("h", dateString, Now)
        If(timeDiff > 5) Then
            aeFlowStatus = 5
        Else
            aeFlowStatus = 1
        End If
    End If
End If

错误一直发生在主Else If约2/3处(Else If(nPing = 1) Then)。我收到了该行的错误。

我尝试将End If放在Else IF之上,然后将Else If更改为If,从而将块分成两个块。当我这样做时它起作用,但我真的不想要两个if语句。

所以,我搞砸了,或者我的口译员有问题吗?

2 个答案:

答案 0 :(得分:3)

在Visual Basic中,“else if”语句是ElseIf而不是Else If。我认为这是你的问题。

答案 1 :(得分:2)

原因是您应该使用ElseIf而不是Else If。看到区别?

现在,更大的问题是你总是很难找到错误的&#34;意大利面条代码&#34;。考虑将代码分解为可重复使用的小方法。这样,您就能够更容易地找出问题所处的位置。更不用说甚至看着所有那些挤在一起的if块都会受到伤害。