excel - 如果计数为 0,函数中带有参数,则隐藏基于多列值的行
问题描述
我需要一个函数,我将过滤的数据范围作为HideRangeIfEmpty
函数中的参数传递。但是,我的代码隐藏了循环中所有为空的行。
仅当过滤可见数据计数(例如 G5、T5、AG5 为零)为零时,预期的输出应该是必须隐藏的行。
Case "UsGe"
Application.ScreenUpdating = False
ActiveSheet.Range("F:BC").EntireColumn.Hidden = True
ActiveSheet.Range("G:G,T:T,AG:AG").EntireColumn.Hidden = False
Range("a1").Select
Call HideRangeIfEmpty("G6:AG200")
Private Sub HideRangeIfEmpty(R1)
For Each xRg In Range(R1)
If xRg.Value = "" Then
xRg.EntireRow.Hidden = True
Else
xRg.EntireRow.Hidden = False
End If
Next xRg
End Sub
解决方案
您可以通过将相关列作为多范围来处理相关列:
Private Sub UsGe()
' ...
Call HideRangeIfEmpty(ActiveSheet.Range("G6:G200, T6:T200, AG6:AG200"))
End Sub
如果任何列的值为 <> "",则将显示该行,否则不显示:
Private Sub HideRangeIfEmpty(ByRef r As Range)
Dim CurrentRow As Long, CurrentArea As Long, CurrentColumn As Long
Dim ToBeShown As Boolean
' Check each row (defined by the first area)
For CurrentRow = 1 To r.Areas(1).Rows.Count
ToBeShown = False ' default: not hidden
' check each area (one or more columns)
For CurrentArea = 1 To r.Areas.Count
' check cell in each column
For CurrentColumn = 1 To r.Areas(CurrentArea).Columns.Count
' If any has a value, then set the marker
If r.Areas(CurrentArea).Cells(CurrentRow, CurrentColumn) <> "" Then _
ToBeShown = True
Next CurrentColumn
Next CurrentArea
' show complete row, if marker is set
r.Areas(1).Cells(CurrentRow, 1).EntireRow.Hidden = Not ToBeShown
Next CurrentRow
End Sub
现在它也适用于像“G6:I200”这样的范围,我的代码将检查列 G、H 和 I。
推荐阅读
- java - RC4 整数加密/解密 Java
- python - python将多个excel中的所有工作表附加到pandas数据框中的有效方法
- node.js - 在将标头发送到客户端后无法设置标头,(但在重新设置标头的位置不明显)
- android - 从后台打开应用程序时,片段中的可序列化 ViewPager 导致 Android 应用程序崩溃
- cassandra - 如何将用户定义的数据类型列表索引为冻结在表“列表”
>”? - python - 有没有办法编辑过滤器 GET 请求返回的对象?
- encoding - 如何在 Jmeter 的 Http 测试脚本记录器中设置默认 UTF-8?
- typescript - Sublimetext 使用键盘快捷键循环方法签名
- android - 如果 Android Spinner 选择不起作用,如何修复它?
- python - 有没有办法删除文本中不在其他文本中的所有单词?