如果内容总和 = 零,如何隐藏 Excel 行并跳过空格?
我有两个代码,如下,一个隐藏行,另一个根据其中包含的值的总和等于零来取消隐藏行。但是,此代码还隐藏了我不想要的空白行,因为它们是部分之间的间隔符。任何建议将不胜感激,如果有改进现有代码的建议,我也将不胜感激。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将每个方法中的 FOR 循环替换为此...。
它首先检查该行范围中的所有单元格是否均为空白,如果为真,则实质上跳过进一步处理。
replace the FOR loop in each method with this....
It first checks to see if all the cells in that row range are blank and essentially skips further processing if true.