编译错误:结束如果没有阻止If

met*_*lah 3 excel vba excel-vba

我目前正在运行以下循环从另一个电子表格中提取信息,但不断收到以下错误消息编译错误:结束如果没有阻止如果,在

ElseIf cel.Offset(0, 8).Value = "" Then wshT.Cells(r, 14).Value = "Physical"
Run Code Online (Sandbox Code Playgroud)

可能导致它的原因以及如何对其进行修复?我的代码如下:

' Loop though cells in column A on main.xlsm
For r = 1 To m

    ' Can we find the value in column A
    Set cel = wshS.Columns(3).Find(What:=wshT.Cells(r, 1).Value, _
    LookAt:=xlWhole, MatchCase:=False)

    If Not cel Is Nothing Then
        If cel.Offset(0, 8).Value = "Yes" Then wshT.Cells(r, 14).Value = "Virtual"
        ElseIf cel.Offset(0, 8).Value = "" Then wshT.Cells(r, 14).Value = "Physical"
        Else: End If
    End If
Next r
Run Code Online (Sandbox Code Playgroud)

Sid*_*out 6

继上面的评论之后,将代码更改为

If Not cel Is Nothing Then
    If cel.Offset(0, 8).Value = "Yes" Then wshT.Cells(r, 14).Value = "Virtual"
    If cel.Offset(0, 8).Value = "" Then wshT.Cells(r, 14).Value = "Physical"
End If
Run Code Online (Sandbox Code Playgroud)

或者对此

If Not cel Is Nothing Then
    If cel.Offset(0, 8).Value = "Yes" Then
        wshT.Cells(r, 14).Value = "Virtual"
    ElseIf cel.Offset(0, 8).Value = "" Then
        wshT.Cells(r, 14).Value = "Physical"
    End If
End If
Run Code Online (Sandbox Code Playgroud)

有关语法IF/EndIf,请参阅下文

'~~> Multiple-line syntax:
If condition [ Then ]
    [ statements ]
[ ElseIf elseifcondition [ Then ]
    [ elseifstatements ] ]
[ Else
    [ elsestatements ] ]
End If

'~~> Single-line syntax:
If Condition Then [ statements ] [ Else [ elsestatements ] ]
Run Code Online (Sandbox Code Playgroud)