没有If的Excel VBA编译错误

时间:2014-02-21 03:06:22

标签: excel excel-vba if-statement vba

我尝试在列“L”和列“V”两者都表示“已完成”时运行代码。然后列“W”将显示“已完成”,否则它将显示“未完成”,但它显示编译错误“Else withou if”。

以下是我的代码

Sub OverallStatus()

Dim x As Long
Dim lastrow As Long

With Sheet1
    If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
         lastrow = .Cells.Find(What:="*", _
                     After:=.Range("A1"), _
                     Lookat:=xlPart, _
                     LookIn:=xlFormulas, _
                     SearchOrder:=xlByRows, _
                     SearchDirection:=xlPrevious, _
                     MatchCase:=False).Row
    Else
        lastrow = 1
    End If
    For x = 2 To lastrow
        If .Range("L" & x) = "Completed" And .Range("V" & x) = "Completed" Then
        .Range("W" & x) = "Completed"
        Else: .Range("W" & x) = "Incomplete"
        End If
    Next
End With
End Sub

1 个答案:

答案 0 :(得分:0)

您的代码需要更改的内容:

    Then之后
  1. 新行字符,或在If
  2. 结束时将其移回
  3. End If
  4. 之前插入 Next
  5. 使用 End If
  6. 替换End Sub上方的End With