当一系列单元格为空时隐藏整行

时间:2015-06-21 14:37:55

标签: excel vba excel-vba

我有一系列细胞B2:AB40。

如果范围内每行中的每个单元格都是空白(我的意思是没有文字或数字,只是颜色填充和边框格式化),我想使用宏隐藏整个行。

e.g。

If every cell in the range B2:AB2 is blank then hide all of row 2.

If every cell in the range B3:AB3 is blank then hide all of row 3

If every cell in the range B4:AB4 is blank then hide all of row 4..etc etc etc

直到并包括第40行。

N.B。在与指定范围相邻的每一行中的A列和AC列中的每个单元格将始终具有文本(分别为某人的名称和公式结果),并且无法更改。

我已经看到了基于单个单元格执行此操作的各种方法,但似乎无法将其用于我的目的。

感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

考虑:

Sub RowHider()
    Dim I As Long, wf As WorksheetFunction
    Set wf = Application.WorksheetFunction
    For I = 2 To 40
        If wf.CountA(Range("B" & I & ":AB" & I)) = 0 Then
            Rows(I).Hidden = True
        Else
            Rows(I).Hidden = False
        End If
    Next I
End Sub

请注意在VBA中使用工作表函数

答案 1 :(得分:0)

试试这个

Sub HideRangeIfEmpty()

  
    
    If Application.WorksheetFunction.CountA(Range("b2:AB2")) = 0 Then
        Range("b2:AB2").EntireRow.Hidden = True

    End If
    
    
End Sub