Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Then
Select Case Target.Value2
Case "ABCP"
Call Macro1
Case "Accounting Policy"
Call Macro2
Case "Audit Committee"
Call Macro3
Case "Auto"
Call Macro4
Case "Auto Issuer Floorplan"
Call Macro5
Case "Auto Issuers"
Call Macro6
Case "Board of Director"
Call Macro7
Case "Bondholder Communication WG"
Call Macro8
Case "Canada"
Call Macro9
Case "Canadian Market"
Call Macro10
End Select
End If
End Sub
这是第一次数据验证的代码。我有另外两个数据验证列表需要具有相同的功能。但是,我无法将数据验证所在的单元格分配给具有此格式的其他代码,或者当前的代码将停止工作。我试图更改Private_sub工作表名称,但它不会做。我该怎么做?
提前谢谢!
答案 0 :(得分:3)
这不是答案!但是评论不允许这么复杂的问题,并且一旦给出澄清就会被删除。(或改为答案)
现在还不清楚你想要什么。如果要执行的宏是相同的那么你可以像在Nathan_Sav的评论中那样做:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Or Target.Address = "$C$3" Then
Select Case Target.Value2
Case "ABCP"
Call Macro1
Case "Accounting Policy"
Call Macro2
....
但是如果他们使用相同的值调用不同的宏,那么你可以这样做:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Or Target.Address = "$C$3" Then
Select Case Target.Value2
Case "ABCP"
If Target.Address = "$B$3" Then Call Macro1 Else Call Macro101
Case "Accounting Policy"
If Target.Address = "$B$3" Then Call Macro2 Else Call Macro102
....
如果它们完全不同,那么你就可以这样做:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Or Target.Address = "$C$3" Then
Select Case Target.Value2
Case "ABCP"
Call Macro1
Case "Accounting Policy"
Call Macro2
....
Case "Canadian Market"
Call Macro10
Case "ABCP-x"
Call Macro101
Case "Accounting Policy-x"
Call Macro102
....
Case "Canadian Market-x"
Call Macro110
End Select
或者像这样分开2个案例:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Then
Select Case Target.Value2
Case "ABCP"
Call Macro1
Case "Accounting Policy"
Call Macro2
....
Case "Canadian Market"
Call Macro10
End Select
ElseIf Target.Address = "$C$3" Then
Select Case Target.Value2
Case "ABCP-x"
Call Macro101
Case "Accounting Policy-x"
Call Macro102
....
Case "Canadian Market-x"
Call Macro110
End Select
如果他们使用相同的宏但使用不同的值,您可以通过Case "ABCP, "some different string"
来执行此操作。但是,如果需要所有验证来评估要调用的宏,则会更改另一个时间。
如果您只是需要知道在宏中更改了哪个单元格来调用,您需要像这样处理它们:(仅举例说明它是如何工作的)
Private Sub Worksheet_Change(ByVal Target As Range)
Call TestMacro(Target)
End Sub
Sub TestMacro(rng as Range)
Debug.Print rng.Address
End Sub
还需要做一些澄清。请帮助我们帮助您