隐藏基于多个单元格的行

时间:2014-07-25 00:53:19

标签: excel vba excel-vba

我需要根据同一行中多个单元格的值隐藏excel中的行。如果我的行包含全0或空白我需要它隐藏。如果有任何整数(不是0或负),我需要显示的行。在同一张纸上,我有“章节标题”,分隔各个部分,下面是一行空白。我可以留下那些故意的空白吗?我已经写了一些部分工作线,我不能一起完成。

Basic layout

希望这有意义并感谢您的帮助!

编辑:现在,如果我有一个隐藏的列,我希望'sum'忽略隐藏的列。我已经尝试在RowanC的答案中嵌套一些东西,但没有去。

For Each myRow In hideRange.Rows
  For Each cell In hideRange.Cells
      If cell.Columns.Hidden = False Then
          Total = Total + cell.Value
      End If
  Next
  If Total = 0 Then 'if the sum of the row=0 then hide
      myRow.EntireRow.Hidden = True
  End If
Next

1 个答案:

答案 0 :(得分:0)

我快点去看看:

Sub rowHider1()
    Dim hideRange As Range
    Dim myRow As Range

    Set hideRange = Sheets(2).Range("A2:D12") 'you must set this to apply to the range you want (you could use active selection if you wanted)
    hideRange.Rows.EntireRow.Hidden = False 'unhide any rows currently hidden
    For Each myRow In hideRange.Rows
        If Application.WorksheetFunction.Sum(myRow) = 0 Then 'if the sum of the row=0 then hide
            myRow.EntireRow.Hidden = True
        End If
    Next
End Sub

更进一步,如果隐藏了一列,请不要将其包含在总和中,远离工作表函数总和:

Sub rowHider()
Dim hideRange As Range
Dim myRow As Range
Dim cell As Range
Dim bob As Double
Set hideRange = Sheets(2).Range("A1:G12") 'you must set this to apply to the range you want (you could use active selection if you wanted)
hideRange.Rows.EntireRow.Hidden = False 'unhide any rows currently hidden
For Each myRow In hideRange.Rows
    bob = 0
    For Each cell In myRow.Cells()
        If cell.EntireColumn.Hidden <> True Then
        Debug.Print cell.Address & "   " & cell.Value
            bob = bob + cell.Value
        End If
    Next
    If bob = 0 Then 'if the sum of the row=0 then hide
        myRow.EntireRow.Hidden = True
    End If
Next
End Sub