是否有隐藏多列空行的代码?

Is there a code to hide empty rows on multiple columns?

我正在寻找使用切换按钮隐藏特定范围列的空行的代码。我有一个从 P9 到 U71 的 table,我想要一个代码来隐藏任何不包含特定 table 中的任何数据的行。我已经尝试了几个代码,最近发布在下面,但是这个代码是根据 U 列中的值隐藏的,即使同一行中有数据但在不同的列中也是如此。我应该指出,我的编码经验为零,因此感谢您对此问题的任何帮助。

Sub Worksheet_Activate()
    Dim r As Range, c As Range
Set r = Range("q9:u71")
Application.ScreenUpdating = False
For Each c In r
      If Len(c.Text) = 0 Then
              c.EntireRow.Hidden = True
      Else
              c.EntireRow.Hidden = False
      End If
 Next c
Application.ScreenUpdating = True
End Sub

请尝试下一个代码:

Sub Worksheet_Activate()
 Dim i As Long, rngH As Range

 For i = 9 To 71
      If WorksheetFunction.CountA(Range("P" & i & ":U" & i)) = 0 Then
              If rngH Is Nothing Then
                   Set rngH = Range("P" & i)
              Else
                   Set rngH = Union(rngH, Range("P" & i))
              End If
      End If
 Next i
 If Not rngH Is Nothing Then rngH.EntireRow.Select '.Hidden = True
End Sub

它只选择要隐藏的行。如果它按您的需要工作,请输入 .Hidden = True 而不是 Select.

它应该非常快,将空单元格放在一个范围内并在最后一次隐藏 EntireRow

现在,如果处理范围内的单元格被代码修改并且一些(已经)隐藏的单元格收到一个值,您不需要使它们可见吗?

我会合上笔记本电脑。如果我的上述问题的答案是肯定的,请使用下一个代码。如果没有,请不要使用它...

Sub Worksheet_Activate()
 Dim r As Range, i As Long, rngH As Range, rngV As Range

 For i = 9 To 71
      If WorksheetFunction.CountA(Range("P" & i & ":U" & i)) = 0 Then
              If rngH Is Nothing Then
                   Set rngH = Range("P" & i)
              Else
                   Set rngH = Union(rngH, Range("P" & i))
              End If
      Else
             If rngV Is Nothing Then
                   Set rngV = Range("P" & i)
              Else
                   Set rngV = Union(rngV, Range("P" & i))
              End If
      End If
 Next i
 If Not rngH Is Nothing Then rngH.EntireRow.Hidden = True
 If Not rngV Is Nothing Then rngV.EntireRow.Hidden = False
End Sub