简体   繁体   English

基于 A 列隐藏/取消隐藏行 (0/"")

[英]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.正如@GSerg 所说,在工作表计算时将触发事件“Worksheet_Calculate”。

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. @Luuk,您遇到错误是因为代码未放入工作表中。 "Me" refers to the object where the code pasted. “我”指的是粘贴代码的对象。 If you put the code in sheet1, then sheet1.Range(...) is meaningful and valid.如果将代码放在 sheet1 中,那么 sheet1.Range(...) 是有意义且有效的。 However, if the code pasted at workbook or a module, workbook.range() or module1.range is invalid.但是,如果代码粘贴在工作簿或模块中,则 workbook.range() 或 module1.range 无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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