如何合并两个子与私人小组Worksheet_Change在工作表上,有不同的触发器
我已经尝试了很多变化(这一切都在网上find,因为我是一个新手),不能'工作。 我哪里错了?
我有一个包含2个单元格的电子表格,可以通过下拉菜单进行更改,对于每个单元格,我需要一个不同的子单元以在更改时激活。 我可以单独做这些工作,但不能把它们结合起来。
你能帮忙吗?
这是我迄今为止:
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
通用模板可能类似于:
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
这是失败的原因是因为你有第一个If块之外的Exit Sub
语句
GoTo Letscontinue End If: End If Exit Sub <----- Remove this
这个Exit Sub
语句总是被命中,因为它在If块之外,所以这行代码
If Not Intersect(Target, Range("B168")) Is Nothing Then ....
将永远达不到。
您应该删除代码底部的Exit Sub
。 因为这将意味着Application.EnableEvents
不会被命中。
Exit Sub <---- Remove this Letscontinue: Application.EnableEvents = True Exit Sub