如果 3 个单元格为空,则隐藏 Excel 中的行

Hide row in Excel if 3 of the cells are blank

我有一个包含 A 到 F 列的 Sheet。我正在寻找 运行 遍历所有行的程序(有没有办法让它只处理活动行?)并检查 D1 & E1 & F1 是否为空,然后隐藏该行(依此类推)。

这是我所拥有的,但效果不是很好....

 Sub Celltest2()
 Dim rw As Range, cel As Range
 Dim i As Integer
 Dim celset As Range

 For Each rw In Sheets("Phonelist").Range("D2:F5000").Rows
    For Each cel In rw.Cells
            If Len(cel.Text) = 0 Then
            cel.EntireRow.Hidden = True

            End If
    Next
Next
 End Sub

试试下面的代码:

Sub Celltest2()

Dim rw As Range, cel As Range
Dim i As Integer
Dim celset As Range
Dim LastRow As Long


With Sheets("Phonelist")
    ' find last row with data in Columns "D, "E" and "F" >> modify to your needs
    LastRow = WorksheetFunction.Max(.Cells(.Rows.Count, "D").End(xlUp).Row, _
                                    .Cells(.Rows.Count, "E").End(xlUp).Row, _
                                    .Cells(.Rows.Count, "F").End(xlUp).Row)

    For Each rw In .Range("D2:F" & LastRow).Rows
        If WorksheetFunction.CountA(Range("D" & rw.Row & ":F" & rw.Row)) = 0 Then
            rw.EntireRow.Hidden = True
        End If
    Next rw
End With

End Sub

选项 2:您可以将上面的循环(以 For Each rw In .Range("D2:F" & LastRow).Rows 开头的循环)替换为以下循环:

For i = 2 To LastRow
    If WorksheetFunction.CountA(Range("D" & i & ":P" & i)) = 0 Then
        Rows(i).EntireRow.Hidden = True
    End If
Next i