如果内容的SUM =零并且跳过空白,我该如何隐藏Excel行?

时间:2011-12-22 21:51:31

标签: excel excel-vba hide rows skip vba

我有两个代码,一个隐藏,另一个根据其中包含的值之和取消隐藏行等于零。但是,此代码还隐藏了我不想要的空白行,因为它们是节之间的间隔符。任何建议都会得到赞赏,如果有改进现有代码的建议,我也会很感激。

Sub HideRows()

Dim R As Long
Dim Rng As Range

    If Selection.Rows.Count > 1 Then
        Set Rng = Selection
    Else
        Set Rng = ActiveSheet.UsedRange
    End If

    For R = 1 To Rng.Rows.Count
        If Application.Sum(Range(Rng(R, 3), Rng(R, Rng.Columns.Count))) = 0# Then
        Rng.Rows(R).Hidden = True
        End If
    Next R

End Sub
Sub UnHideRows()

Dim R As Long
Dim Rng As Range

    If Selection.Rows.Count > 1 Then
        Set Rng = Selection
    Else
        Set Rng = ActiveSheet.UsedRange
    End If

    For R = 1 To Rng.Rows.Count
        If Application.Sum(Range(Rng(R, 3), Rng(R, Rng.Columns.Count))) = 0# Then
        Rng.Rows(R).Hidden = False
        End If
    Next R

End Sub

1 个答案:

答案 0 :(得分:4)

用这个....替换每个方法中的FOR循环。

myRange = Range(Rng(R, 3), Rng(R, Rng.Columns.Count))
For R = 1 To Rng.Rows.Count
  If Application.CountBlank(myRange) <> myRange.Cells.Count Then
    If Application.Sum(myRange) = 0# Then
      Rng.Rows(R).Hidden = True
    End If
Next R

首先检查该行范围内的所有单元格是否为空白,如果为真,则基本上跳过进一步处理。