且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

识别并突出显示Excel VBA中的空白行

更新时间:2023-11-04 14:25:04

我使用了

I've used a COUNTBLANK function on the first 20 columns of each row to determine if any blank cells exist.

Dim rng As Range, lCount As Long, LastRow As Long

With ActiveSheet    'set this worksheet properly!
    LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
    For Each rng In .Range("A1:A" & LastRow)
        With rng.Resize(1, 20)
            If Application.CountBlank(.Cells) = 20 Then  'All 20 cells are blank
                lCount = lCount + 1
                .EntireRow.ClearContents
                .EntireRow.Interior.ColorIndex = 6
            End If
        End With
    Next rng
End With

If lCount > 0 Then
    MsgBox "Data contains blank row(s): " & lCount
Else
    MsgBox "No blank Rows"
End If

如果所有20个单元格均为空白,则将整行设置为空白,并应用黄色突出显示.

If all 20 cells are blank then the entire row is made blank and yellow highlighting is applied.

我正在使用 COUNTBLANK函数,因为尚不清楚公式是否返回零长度的字符串.COUNTBLANK会将其计为空白.

I'm using the COUNTBLANK function as it was not clear on whether you have zero-length strings returned by formulas. COUNTBLANK will count these as blanks.