在select更改中嵌套两个IF语句,包括exit sub和call module

我想把这两个代码合并成1:

Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Intersect(Target, Range("AL1")) Is Nothing Then Exit Sub Application.Run "Module2.Sub1" end sub 

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Intersect(Target, Range("AL2")) Is Nothing Then Exit Sub Application.Run "Module2.Sub2" end sub 

这样做取消了第二次IF声明,但继续工作的第一个

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Intersect(Target, Range("AL1")) Is Nothing Then Exit Sub Application.Run "Module2.Sub1" If Intersect(Target, Range("AL2")) Is Nothing Then Exit Sub Application.Run "Module2.Sub2" end sub 

我在网上find了copde snippet,我想我不需要End If如果因为退出Sub …但是我真的需要帮助才能让我的头在这个附近。 帮帮我?

我这样做是为了能够获得运行我的macros的形状的工具提示。

问题是与Exit Sub令牌。 当你selectAL2 ,第一行返回Nothing并且退出子。 试试这个:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not Intersect(Target, Range("AL1")) Is Nothing Then Application.Run "Module2.Sub1" If Not Intersect(Target, Range("AL2")) Is Nothing Then Application.Run "Module2.Sub2" End Sub 

请注意,如果您select两个单元格,那么这两个潜艇将运行。

尝试:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not Intersect(Target, Range("AL1")) Is Nothing Then Application.Run "Module2.Sub1" If Not Intersect(Target, Range("AL2")) Is Nothing Then Application.Run "Module2.Sub2" End Sub