隐藏和取消隐藏excel中的行

时间:2014-12-10 20:17:48

标签: excel-vba vba excel

这是我正在使用的代码,用于隐藏包含0的行,并在不为0时取消隐藏。如何更改代码以使之前隐藏的行在更改为未隐藏的前一天之前隐藏?

Sub Test()
    Dim c As Range
    For Each c In Range("A2:A201")
        If c.Value = "0" Then
            c.EntireRow.Hidden = True
        Else
            c.EntireRow.Hidden = False
        End If
    Next c
 End Sub

谢谢!

2 个答案:

答案 0 :(得分:0)

没有足够的细节可以100%确定这是你所追求的,但希望它能给出这个想法,你可以根据自己的意愿进行调整。正如评论中所提到的(我还没有评论,或者会添加评论),要实现这一点,您必须存储之前隐藏行的日期。您可以通过将日期添加到同一行上的单元格来执行此操作,如果愿意,可以隐藏存储日期的列。我的解决方案不会隐藏日期隐藏列,并将其写入相邻单元格,以调整此值,只需更改偏移值

Sub HideTodaysZeroValues()
    Dim c As Range
    Dim OffsetStoredDateColumnNumber As Integer ' makes it easier to edit the column number
    OffsetStoredDateColumnNumber = 1
    For Each c In Range("A2:A201")
        If c.Value = "0" Then ' If Col A is zero
            'Check if stored date column is populated
            If Len(c.Offset(0, OffsetStoredDateColumnNumber).Value) > 0 Then
                'if it is populated, compare date with now, and if its equal to 1, show it
                If DateDiff("d", c.Offset(0, 1).Value, Now()) = 1 Then
                    c.EntireRow.Hidden = False
                Else
                    'else hide it and store the date it was hidden
                    'As its stands right now this code will only unhide the rows from yesterday, any older rows will be rehiddne
                    'If this is not desired, change the "= 1" above, to be " > 0"
                    c.Offset(0, OffsetStoredDateColumnNumber).Value = Now()
                    c.EntireRow.Hidden = True
                End If
            Else
                'comes here if stored date is not populated, which will happen first time a row needs to be hidden
                c.Offset(0, OffsetStoredDateColumnNumber).Value = Now()
                c.EntireRow.Hidden = True
            End If
        Else
            c.EntireRow.Hidden = False
        End If
    Next c
 End Sub

答案 1 :(得分:-1)

您只需要检查行是否隐藏,然后取消隐藏:

Sub Test_unhide()
Dim c As Range
For Each c In Range("A2:A201")
    If c.EntireRow.Hidden = True Then
        c.EntireRow.Hidden = False
    End If
Next c
End Sub