Excel VBA Else没有if

HPM*_*HPM 0 excel vba excel-vba

我想在两个场景中使用if-function来进行distingiush.

For Each Cell In Tabelle3.Range("A" & lastrow2)

Option A: If Cell <> "" Then run code
Option B: If Cell = "" Then skip this empty cell and go on with the next one
Run Code Online (Sandbox Code Playgroud)

这里整个代码:

Sub IfFunction()

Dim lastrow2 As Long
lastrow2 = Tabelle3.Range("A" & Rows.Count).End(xlUp).Row
Set myrange2 = Tabelle8.UsedRange


    For i = 2 To lastrow2

    For Each Cell In Tabelle3.Range("A" & lastrow2)

    If Cell = "" Then i = i + 1

    Else: i = i



        Tabelle3.Cells(7 + i, 19) = Application.WorksheetFunction.VLookup(Tabelle3.Cells(7 + i, 1), myrange2, 3, False)



        Tabelle3.Cells(7 + i, 20) = Application.WorksheetFunction.VLookup(Tabelle3.Cells(7 + i, 1), myrange2, 4, False)



        Tabelle3.Cells(7 + i, 21) = Application.WorksheetFunction.VLookup(Tabelle3.Cells(7 + i, 1), myrange2, 5, False)

        Next i

    End If


End Sub
Run Code Online (Sandbox Code Playgroud)

当我尝试运行此代码时,它不会执行,因为出现错误,即存在'没有IF'功能的ELSE.

有谁知道如何在这里使用IF功能或使用什么?谢谢.:)

Pᴇʜ*_*Pᴇʜ 5

如果你继续写,Then这意味着该If语句只包含一行:

If Cell = "" Then i = i + 1 'End If automatically here
Run Code Online (Sandbox Code Playgroud)

那么Else也必须在那条线上:

If Cell = "" Then i = i + 1 Else i = i 'End If automatically here
Run Code Online (Sandbox Code Playgroud)

如果要使用多行If语句

If Cell = "" Then 
    i = i + 1
Else
    i = i
End If
Run Code Online (Sandbox Code Playgroud)

但......

因为i = i没有做任何你可以写的东西

If Cell = "" Then i = i + 1
Run Code Online (Sandbox Code Playgroud)

Else完全省略该部分,因为它什么都不做.


另一个是...

因为你使用的是For iNext i增量i自动和你不需要给自己增加它.没有i = i + 1必要