为什么我得到了#34;声明的结尾"在我的Excel VBA代码中编译错误?

时间:2017-06-06 04:41:53

标签: vba excel-vba compiler-errors excel

以下是我在VBA中的代码:

Sub Hamza_Starting_to_Learn()

Dim Hamza_Variable As Long

Hamza_Variable = 7

If Hamza_Variable = 7 Then

MsgBox Hamza_Variable & " Is da bomb"

Elself Hamza_Variable > 7 Then

MsgBox Hamza_Variable & " Is da bigger bomb"

Else

MsgBox Hamza_Variable & "We got ourselves a problem here bro"

End If

它不断给出编译错误"语句结束"在以下行:

Elself Hamza_Variable > 7 Then

突出显示Then并提供此错误。

3 个答案:

答案 0 :(得分:1)

既然你开始学习,也许你应该用Select Case声明来获得familair:

Option Explicit

Sub Hamza_Starting_to_Learn()

Dim Hamza_Variable As Long

Hamza_Variable = 7

Select Case Hamza_Variable
    Case 7
        MsgBox Hamza_Variable & " Is da bomb"
    Case Is > 7
        MsgBox Hamza_Variable & " Is da bigger bomb"
    Case Else
        MsgBox Hamza_Variable & "We got ourselves a problem here bro"

End Select

End Sub

答案 1 :(得分:0)

试试这个:

Sub Hamza_Starting_to_Learn()

    Dim Hamza_Variable As Long

        Hamza_Variable = 7

    If (Hamza_Variable = 7) Then

        MsgBox Hamza_Variable & " Is da bomb"

    ElseIf Hamza_Variable > 7 Then

        MsgBox Hamza_Variable & " Is da bigger bomb"

    Else

        MsgBox Hamza_Variable & "We got ourselves a problem here bro"

    End If

End Sub

您没有使用End Sub

结束子例程

答案 2 :(得分:0)

在原始代码中,您在收到错误的行中写了Elself而不是ElseIf(请注意拼写错误lI)。这可能会让VBA感到困惑,反过来又会给你带来误导性的错误信息。

(感觉不是太糟糕。Other people have also had problems distinguishing these two letters in some monospace fonts before.