使用VBA Excel隐藏其行具有零值的列

时间:2017-09-06 09:22:26

标签: excel vba hide hidden

我有一张Excel工作表,其值为A,B,C,D和F列中的数字(这些列有多行)。我想要的只是当点击一个按钮时,其行的值为零的所有列都将被隐藏。

所以,如果有人有解决方案,请帮助我!非常感谢你

2 个答案:

答案 0 :(得分:0)

Option Explicit
Sub HideZeros()
  HideCells (1)
End Sub
Sub HideCells(Col_Num As Integer)
Dim Row_num As Integer
For Row_num = 1 To 10
  If Sheet1.Cells(Row_num, Col_Num).Value = 0 Then Sheet1.Cells(Row_num, Col_Num).Delete Shift:=xlUp
Next
End Sub

以上是指导您按照自己的意愿进行修改的正确方向。它将删除工作表1列A的前10行中具有0值的任何单元格,然后移动其余单元格。 您无法隐藏一个只能隐藏行/列的单元格。所以删除是你唯一的选择...希望它有所帮助

答案 1 :(得分:0)

我根据你想要做的事情创建了两个解决方案。在代码中,我已经解释了每个步骤正在做什么,其余的代码只是针对不同的列重复。 第一个隐藏列,如果该列中的行等于零:

    Sub HideRowsWhereAnyRowIsZero()

    Dim i As Long 'Declarations

    For i = 1 To Rows.Count 'Find the last row and loop through until reached
        If Cells(i, 1).Value = "0" Then 'if the cell equals 0 then move to next step if not move to next cell
                Cells(i, 1).EntireColumn.Hidden = True 'hide column
                    If Cells(i, 1).EntireColumn.Hidden = True Then Exit For 'if column has been hidden then break the loop
        End If 'if cell doesnt  equal 0 move on
    Next i 'creates loop to go thorugh each row in column


    For i = 1 To Rows.Count
        If Cells(i, 2).Value = "0" Then
            Cells(i, 2).EntireColumn.Hidden = True
                If Cells(i, 2).EntireColumn.Hidden = True Then Exit For
        End If
    Next i

    For i = 1 To Rows.Count
        If Cells(i, 3).Value = "0" Then
            Cells(i, 3).EntireColumn.Hidden = True
                If Cells(i, 3).EntireColumn.Hidden = True Then Exit For
        End If
    Next i


    For i = 1 To Rows.Count
        If Cells(i, 4).Value = "0" Then
            Cells(i, 4).EntireColumn.Hidden = True
                If Cells(i, 4).EntireColumn.Hidden = True Then Exit For
        End If
    Next i


    For i = 1 To Rows.Count
        If Cells(i, 6).Value = "0" Then
            Cells(i, 6).EntireColumn.Hidden = True
                If Cells(i, 6).EntireColumn.Hidden = True Then Exit For
        End If
    Next i

    End Sub

下一段代码只隐藏列中的所有行都为0

Sub HideColumnsWhenAllRowsAreZero()

Dim Lastrow As Long

Lastrow = Range("A" & Rows.Count).End(xlUp).row 'finds the last row for column A

    If WorksheetFunction.Sum(Range("A1:A" & Lastrow)) = 0 Then 'sum the values of the column if it equals 0 then move on to hide column

        Range("A1:A" & Lastrow).EntireColumn.Hidden = True 'Hides column

    End If 'Ends statement for column

Lastrow = Range("B" & Rows.Count).End(xlUp).row

    If WorksheetFunction.Sum(Range("B1:B" & Lastrow)) = 0 Then

        Range("B1:B" & Lastrow).EntireColumn.Hidden = True

    End If

Lastrow = Range("C" & Rows.Count).End(xlUp).row

       If WorksheetFunction.Sum(Range("C1:C" & Lastrow)) = 0 Then

        Range("C1:C" & Lastrow).EntireColumn.Hidden = True

    End If

Lastrow = Range("D" & Rows.Count).End(xlUp).row

    If WorksheetFunction.Sum(Range("D1:D" & Lastrow)) = 0 Then

        Range("D1:D" & Lastrow).EntireColumn.Hidden = True

    End If

Lastrow = Range("F" & Rows.Count).End(xlUp).row

    If WorksheetFunction.Sum(Range("F1:F" & Lastrow)) = 0 Then

        Range("F1:F" & Lastrow).EntireColumn.Hidden = True

    End If


End Sub

我建议踩踏代码(使用F8)并尝试了解实际发生的情况。