这是我的工作表代码。如果我在 D32 中输入一些内容,它就会起作用,但如果公式自动更改数字,它就不会执行任何操作。我做错了什么?我基本上将它设置为如果单元格为零则隐藏行,如果单元格为正数则显示行。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("D32")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
' Display a message when one of the designated cells has been
' changed.
' Place your code here.
If Range("D32").Value = "0" Then
Rows("32:32").EntireRow.Hidden = True
ElseIf Range("D32").Value = "<>0" Then
Rows("32:32").EntireRow.Hidden = False
End If
End If
End Sub
答案1
马修,试试这个代码,功能齐全。
Private Sub Worksheet_Calculate()
Dim KeyCells As Range
Set KeyCells = Range("C1")
If Range("C1").Value = 0 Then
Rows("3:5").EntireRow.Hidden = True
ElseIf Range("C1").Value <> 0 Then
Rows("3:5").EntireRow.Hidden = False
End If
End Sub
注意:根据需要调整单元格/行引用。