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

时间:2016-10-05 13:41:46

标签: excel-vba hide vba excel

我有一张带有A到F列的工作表。我正在寻找运行所有行的程序(有没有办法让它只执行活动的行?)并检查是否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

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

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