如果选择与命名范围相交,则选择该命名范围 [英] If selection intersects with a named range then select that named range

查看:65
本文介绍了如果选择与命名范围相交,则选择该命名范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个计划是对每个命名范围重复此操作,直到我意识到这将是多少.

My first plan was to repeat this for every named range, until i realized how much that would be.

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

推荐答案

首先,您需要将 activecell 更改为 target ,因为 target 是静态的调用范围.另外,只需在 IF 中添加 NOT 条件,这样就不必使用 ELSE

First you'll want to change that activecell to target since target is static as the calling range. Also, just add a NOT condition to your IF so you don't have to use the ELSE

如果要针对一系列命名范围测试 target 以查看 target 是否与至少一个命名范围相交,则可以使用应用程序的联合方法:

If you wanting to test target against a list of named ranges to see if target intersects at least one of the named ranges, you can use the application's union method:

If Not Intersect(Target, Union(Range("M_1"), Range("M_2"), Range("M_3")) Is Nothing Then

如果您需要更多控制,也可以执行循环:

You could also do a loop if you need more control:

 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

这篇关于如果选择与命名范围相交,则选择该命名范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆