答案 0 :(得分:1)
这样的事情应该有效
Option Explicit 'Very first line to ensure that variables are declared
Private Sub Worksheet_Change(ByVal Target As Range)
'check if target address is B1
If Target.Address(RowAbsolute:=False, ColumnAbsolute:=False) = "B1" Then
ShowHideRows
End If
End Sub
Public Sub ShowHideRows()
If Range("B1").Value = "Delete" Then
Rows("3:4").EntireRow.Hidden = True
Rows("7:8").EntireRow.Hidden = False
ElseIf Range("B1").Value = "Open" Then 'use ElseIf if possible like here
Rows("3:4").EntireRow.Hidden = False
Rows("7:8").EntireRow.Hidden = True
End If
End Sub
注意:这是区分大小写的。因此,如果您键入B1 delete
而不是Delete
,它将无法运行。要使其不区分大小写,请使用LCase()
,如:
LCase(Range("B1").Value) = "delete" 'string delete must be lower case!