我写了以下宏:使用公式填充单元格e3并可以更改值。但是,要使此宏正常工作,我必须按e3中的Enter键将其触发。我希望在单击“单元说明”中的选项卡时触发该宏,并认为我需要另一个宏才能触发它。我尝试了一个宏(在底部称为第二个宏),但这没有用。
任何人都可以帮忙。
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("E3")) Is Nothing Then
If UCase$(Range("E3").Value) = "DWW" Then
Rows("6:47").EntireRow.Hidden = False 'if "DWW" is entered in E3, rows
are visible
Else
Rows("6:47").EntireRow.Hidden = True 'if anything other than "DWW" is
entered in E3 or E3 is blank, the rows are hidden
End If
End If
If Not Intersect(Target, Range("E3")) Is Nothing Then
If UCase$(Range("E3").Value) = "THETIS" Then
Rows("48:70").EntireRow.Hidden = False 'if "Thetis" is entered in E3,
rows are visible
Else
Rows("48:70").EntireRow.Hidden = True 'if anything other than "Thetis"
is entered in E3 or E3 is blank, the rows are hidden
End If
End If
If Not Intersect(Target, Range("E3")) Is Nothing Then
If Range("E3").Value = "" Then
Rows("71:75").EntireRow.Hidden = False 'if "DWW" is entered in E3, rows
are visible
Else
Rows("71:75").EntireRow.Hidden = True 'if anything other than "THETIS"
is entered in E3 or E3 is blank, the rows are hidden
End If
End If
End Sub
'second macro
private sub SecondMacro()
call Worksheet_change
end sub
答案 0 :(得分:1)
您正在寻找Worksheet_Activate方法。只要选择了标签,它就会运行。
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("E3")) Is Nothing Then
Call runYourThing 'When the cell change event is triggered, only run your thing if the changed range includes cell E3
End If
End Sub
Private Sub Worksheet_Activate()
Call runYourThing 'When the worksheet activate event is triggered, always run your thing
End Sub
Sub runYourThing() 'Run the thing
If UCase$(Range("E3").Value) = "DWW" Then
Rows("6:47").EntireRow.Hidden = False 'if "DWW" is entered in E3, rows are Visible
Else
Rows("6:47").EntireRow.Hidden = True 'if anything other than "DWW" is entered in E3 or E3 is blank, the rows are hidden
End If
If UCase$(Range("E3").Value) = "THETIS" Then
Rows("48:70").EntireRow.Hidden = False 'if "Thetis" is entered in E3, rows are visible
Else
Rows("48:70").EntireRow.Hidden = True 'if anything other than "Thetis" is entered in E3 or E3 is blank, the rows are hidden
End If
If Range("E3").Value = "" Then
Rows("71:75").EntireRow.Hidden = False 'if "DWW" is entered in E3, rows are Visible
Else
Rows("71:75").EntireRow.Hidden = True 'if anything other than "THETIS" is entered in E3 or E3 is blank, the rows are hidden
End If
End Sub
答案 1 :(得分:1)
请注意,您可以在此处使用3种不同的事件
Worksheet_Activate()
如果工作表已激活(如果您从另一个工作表切换到该工作表),则运行。
Worksheet_Change(ByVal Target As Range)
如果单元格中的值发生更改,则运行。
Worksheet_SelectionChange(ByVal Target As Range)
如果您选择其他单元格,则运行
因此,您可以决定最适合您的活动。
答案 2 :(得分:1)
在我读取宏的方式中,您似乎希望某些行根据E3
单元格值的更改而自动隐藏。如果是正确的话,那么我认为对您的代码进行以下替换将起作用:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("E3")) Is Nothing Then checkE3
End Sub
Sub checkE3()
Dim E3val As String
E3val = UCase$(Range("E3").Value)
Rows("6:47").EntireRow.Hidden = Not (E3val = "DWW")
Rows("48:70").EntireRow.Hidden = Not (E3val = "THETIS")
Rows("71:75").EntireRow.Hidden = Not (E3val = "")
End Sub
如果E3的值由于用户自己更改而更改,则会“触发” ,但是如果E3中存在一个公式,该公式在重新计算时会导致更改,则不会触发。
在这种情况下,您可能希望对其他事件(例如checkE3
)运行Worksheet_Calculate()
。基本上,每次执行一次重新计算时,这将导致它运行的次数远远超过您的需要,但是checkE3中使用的资源非常小,因此除非出现 lot ,否则您不太可能看到任何问题。 >进行计算。
Private Sub Worksheet_Calculate()
checkE3
End Sub