Excel中的工作表更改

这是该文件的屏幕截图。 在这里输入图像说明

我在VBA中使用下面的代码来自动隐藏一些特定单元格中的值的更改行。

Private Sub Worksheet_Change(ByVal Target As Range) Dim cell As Range Set cell = Range("G1") If Not Application.Intersect(cell, Range(Target.Address)) Is Nothing Then If Range("G1").Value > 50 Then Rows("12:17").EntireRow.Hidden = False Else Rows("12:17").EntireRow.Hidden = True End If End If End Sub 

我已将选项更改为启用macros。 代码应该可以工作,但不能到达那里。

在VBE中,点击[ctrl] + G进入立即窗口并将其粘贴到Application.EnableEvents = True然后在行尾input。

你的代码可以略微修剪到下面。

 Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("G1")) Is Nothing Then Rows("12:17").EntireRow.Hidden = CBool(Range("G1").Value <= 50) End If End Sub 

确保您在工作表代码表中,而不是ThisWorkbook代码表或模块代码表。 右键单击工作表的名称选项卡并select“ 查看代码”是确保您位于正确位置的最快捷方式。