简体   繁体   中英

Hide/unhide rows based on Column A (0/"")

I found this code, and I put it in the worksheet code, saved and exited and it doesn't do anything on open. Thank you for your time.

Private Sub Worksheet_Calculate()
Dim c As Range
Application.ScreenUpdating = False
For Each c In Me.Range("A1:A80")
    If c.Value = 0 Or c.Value = "" Then
        c.EntireRow.Hidden = True
    Else
        c.EntireRow.Hidden = False
    End If
Next c
Application.ScreenUpdating = True
End Sub

As @GSerg said, the event "Worksheet_Calculate" will trigger when the worksheet calculation.

For the original question, it is related to the worksheet only. It seems that it should be triggered once the worksheet is activated. It can be referred by the following code:

Private Sub Worksheet_Activate()
Dim c As Range
Application.ScreenUpdating = False
For Each c In Me.Range("A1:A80")
    If c.Value = 0 Or c.Value = "" Then
        c.EntireRow.Hidden = True
    Else
        c.EntireRow.Hidden = False
    End If
Next c
Application.ScreenUpdating = True
End Sub

@Luuk, you encounter the error because the code is not put into a worksheet. "Me" refers to the object where the code pasted. If you put the code in sheet1, then sheet1.Range(...) is meaningful and valid. However, if the code pasted at workbook or a module, workbook.range() or module1.range is invalid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM