多行重复代码

Repeat code for multiple rows

下面的 VBA 代码非常适合一行。我需要它循环 100 行并对每一行执行相同的工作。有人可以帮我做这个吗?

Sub IsEmptyRange()

Dim cell As Range

Dim bIsEmpty As Boolean

bIsEmpty = False

For Each cell In Range("A1:H1")

    If IsEmpty(cell) = True Then

        bIsEmpty = True

        Exit For

    End If

Next cell

If bIsEmpty = True Then

    '**PLACE CODE HERE**

    [I1].Value = "Empty Cells"

Else

    '**PLACE CODE HERE**

    [I1].Value = "Complete"

End If

End Sub

谢谢!!

你能尝试实现这个吗?

* = things added/changed (remove when you put in VBA)

*Dim lRow as Integer
*Dim i as Integer

*lRow = Cells(Rows.Count, 1).End(xlUp).Row

*For i = 1 to lRow
For Each cell In Range("A" & i & ":H" & i)

If IsEmpty(cell) = True Then

    bIsEmpty = True

    Exit For

End If

Next cell

If bIsEmpty = True Then

'**PLACE CODE HERE**

*[I & i].Value = "Empty Cells"

Else

'**PLACE CODE HERE**

*[I & i].Value = "Complete"

End If
*Next i

这是否需要 VBA?看起来您可以在单元格 i1 中使用此公式并向下复制:=IF(COUNTBLANK(A1:H1)>0,"Empty Cells","Complete")

如果它绝对必须是 VBA,那么这对你有用:

Sub tgr()

    Dim ws As Worksheet
    Dim lLastRow As Long

    Set ws = ActiveWorkbook.ActiveSheet
    On Error Resume Next
    lLastRow = ws.Range("A:H").Find("*", ws.Range("A1"), xlValues, xlPart, , xlPrevious).Row
    On Error GoTo 0
    If lLastRow = 0 Then Exit Sub   'No data

    With ws.Range("I1:I" & lLastRow)
        .Formula = "=IF(COUNTBLANK(A" & .Row & ":H" & .Row & ")>0,""Empty Cells"",""Complete"")"
        .Value = .Value
    End With

End Sub