如果内容总和 = 零,如何隐藏 Excel 行并跳过空格?

发布于 2024-12-22 22:27:59 字数 860 浏览 1 评论 0原文

我有两个代码,如下,一个隐藏行,另一个根据其中包含的值的总和等于零来取消隐藏行。但是,此代码还隐藏了我不想要的空白行,因为它们是部分之间的间隔符。任何建议将不胜感激,如果有改进现有代码的建议,我也将不胜感激。

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

I have two codes, below, one that hides and the other that unhides rows based on the sum of values contained therein equaling zero. However, this code also hides blank rows which I do not want since they are spacers between sections. Any advice would be apprecated, and if there are recommendations to improve the existing code I would appreciate that as well.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木槿暧夏七纪年 2024-12-29 22:27:59

将每个方法中的 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

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

replace the FOR loop in each method with this....

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

It first checks to see if all the cells in that row range are blank and essentially skips further processing if true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文