我的第一个计划是为每个命名的范围重复这个,直到我意识到它会有多少。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(ActiveCell, Range("M_1")) Is Nothing Then
Else
Range("M_1").Select
End If
End Sub
答案 0 :(得分:2)
首先,您要将activecell
更改为target
,因为target
是静态的作为调用范围。另外,只需在NOT
添加IF
条件,这样您就不必使用ELSE
如果要针对命名范围列表测试target
以查看target
是否与至少一个命名范围相交,则可以使用应用程序union
方法:
If Not Intersect(Target, Union(Range("M_1"), Range("M_2"), Range("M_3")) Is Nothing Then
如果你需要更多控制,你也可以做一个循环:
doIntersect = false
rngCounter = 0
For each strTestRange in Array("M_1", "M_2", "M_3")
If Not Intersect(Target, Range(strTestRange) Is Nothing Then
doIntersect = true
rngCounter = rngCounter + 1
End if
Next strTestRange
If doIntersect Then
msgbox(rngCounter & " named ranges intersect your selection")
Else
msgbox("None of the named ranges intersected your selection")
End if
答案 1 :(得分:1)
添加循环以遍历命名范围:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim nm As Name
Dim nmStr As String
For Each nm In ThisWorkbook.Names
nmStr = nm.Name
If Not Intersect(Target, Range(nmStr)) Is Nothing Then
Application.EnableEvents = False
Range(nmStr).Select
Application.EnableEvents = True
End If
Next nm
End Sub