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

时间:2014-03-22 16:46:38

标签: excel vba excel-vba

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

ElseIf cel.Offset(0, 8).Value = "" Then wshT.Cells(r, 14).Value = "Physical"

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

' 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

1 个答案:

答案 0 :(得分:5)

根据我上面的评论,将代码更改为

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

或者

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

有关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 ] ]