单击工作表触发工作表宏
Triggering Worksheet Macro on click of worksheet
我编写了以下宏:使用公式填充单元格 e3 并可以更改值。但是,为了使这个宏起作用,我必须在 e3 中按回车键来触发它。我希望在单击名为 "unit specification" 的选项卡时触发此宏,并且我相信我需要第二个宏来触发它。我试过一个宏(在底部称为第二个宏),但这没有用。
谁能帮忙。
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
您正在寻找 Worksheet_Activate 方法。 This'll 运行 whenever the tab is selected.
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
请注意,您可以在此处使用 3 种不同的事件
Worksheet_Activate()
如果工作 sheet 被激活(如果你从另一个 sheet 切换到这个 sheet)则运行。
Worksheet_Change(ByVal Target As Range)
如果单元格中的值更改,则运行。
Worksheet_SelectionChange(ByVal Target As Range)
如果你 select 另一个单元格
运行
这样您就可以决定哪个活动最适合您。
根据我阅读您的宏的方式,您似乎希望某些行根据 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 的值由于用户自己更改而发生变化,这会'fire'但不会如果 E3 中有一个重新计算的公式,导致变化。
在这种情况下,您可能想要 运行 checkE3
其他事件,例如 Worksheet_Calculate()
。这将导致它 运行 的频率远远超过您的需要,基本上每次执行重新计算时,但 checkE3 中使用的资源非常小,因此您不太可能看到任何问题,除非有 lot 计算正在进行中。
Private Sub Worksheet_Calculate()
checkE3
End Sub
我编写了以下宏:使用公式填充单元格 e3 并可以更改值。但是,为了使这个宏起作用,我必须在 e3 中按回车键来触发它。我希望在单击名为 "unit specification" 的选项卡时触发此宏,并且我相信我需要第二个宏来触发它。我试过一个宏(在底部称为第二个宏),但这没有用。
谁能帮忙。
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
您正在寻找 Worksheet_Activate 方法。 This'll 运行 whenever the tab is selected.
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
请注意,您可以在此处使用 3 种不同的事件
Worksheet_Activate()
如果工作 sheet 被激活(如果你从另一个 sheet 切换到这个 sheet)则运行。Worksheet_Change(ByVal Target As Range)
如果单元格中的值更改,则运行。Worksheet_SelectionChange(ByVal Target As Range)
如果你 select 另一个单元格 运行
这样您就可以决定哪个活动最适合您。
根据我阅读您的宏的方式,您似乎希望某些行根据 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 的值由于用户自己更改而发生变化,这会'fire'但不会如果 E3 中有一个重新计算的公式,导致变化。
在这种情况下,您可能想要 运行 checkE3
其他事件,例如 Worksheet_Calculate()
。这将导致它 运行 的频率远远超过您的需要,基本上每次执行重新计算时,但 checkE3 中使用的资源非常小,因此您不太可能看到任何问题,除非有 lot 计算正在进行中。
Private Sub Worksheet_Calculate()
checkE3
End Sub