[英]How to merge two subs with Private Sub Worksheet_Change on sheet, which have different triggers
I have tried a lot of variations for this ( all found online, since I am a novice on this) and cant' get it to work. 我为此尝试了很多变体(所有都是在线上找到的,因为我是这个新手)并且无法使其正常工作。 Where am I going wrong?
我要去哪里错了?
I have a spreadsheet with 2 cells that can be changed with drop down, for each cell I need a different sub to activate on change. 我有一个包含2个单元格的电子表格,可以通过下拉菜单进行更改,对于每个单元格,我需要一个不同的子项来激活更改。 I can make each of these work individually, but not on combining them.
我可以单独完成所有这些工作,但不能合并使用。
Can you help, please? 你能帮忙吗?
This is what I have so far: 这是我到目前为止的内容:
Private Sub Worksheet_Change(ByVal Target As Range)
'hides currencies that aren't required
'On Error GoTo 99
If Not Intersect(Target, Range("B8")) Is Nothing Then
Columns("F:F").EntireColumn.Hidden = False
Columns("E:E").EntireColumn.Hidden = False
Application.EnableEvents = False
If Range("B8").Text = "" Then
Columns("F:F").EntireColumn.Hidden = True
Columns("E:E").EntireColumn.Hidden = False
GoTo Letscontinue
Else
Columns("F:F").EntireColumn.Hidden = False
Columns("E:E").EntireColumn.Hidden = True
GoTo Letscontinue
End If: End If
Exit Sub
'adds new lines for addenda during contract live cycle
If Not Intersect(Target, Range("B168")) Is Nothing Then
If Range("B168").Text = "Yes" Then
Range("B172").Select
If Range("B172").Text = "Yes" Then
Range("B176").Select
If Range("B176").Text = "Yes" Then
Range("B180").Select
If Range("B176").Text = "Yes" Then
Else
ActiveCell.Offset(-2, 0).Rows("1:4").EntireRow.Select
Selection.Copy
ActiveCell.Offset(4, 0).Rows("1:1").EntireRow.Select
Selection.Insert Shift:=xlDown
ActiveCell.Offset(3, 1).Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=R[-4]C+1"
Selection.ClearContents
ActiveCell.FormulaR1C1 = "No"
ActiveCell.Offset(1, 0).Range("A1").Select
GoTo Letscontinue
End If: End If: End If: End If: End If
Exit Sub
Letscontinue:
Application.EnableEvents = True
Exit Sub
99:
Resume Letscontinue
End Sub
A general template could resemble: 通用模板可能类似于:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("B8")) Is Nothing Then
'
' your code
'
End If
If Not Intersect(Target, Range("B168")) Is Nothing Then
'
' your code
'
End If
Application.EnableEvents = True
End Sub
The reason this is failing is because you have an Exit Sub
statement outside your first If block 失败的原因是因为您的第一个If块之外有一个
Exit Sub
语句
GoTo Letscontinue
End If: End If
Exit Sub <----- Remove this
This Exit Sub
statement will always be hit as it is outside of the If block, so this line of code 该
Exit Sub
语句将始终被命中,因为它位于If块之外,因此此行代码
If Not Intersect(Target, Range("B168")) Is Nothing Then ....
will never be reached. 永远不会达到。
You should remove the Exit Sub
at the bottom of your code as well. 您还应该删除代码底部的
Exit Sub
。 As this will mean that the Application.EnableEvents
will not be hit. 因为这将意味着不会单击
Application.EnableEvents
。
Exit Sub <---- Remove this
Letscontinue:
Application.EnableEvents = True
Exit Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.