【问题标题】:How to check if a cell belongs to a dynamic name range如何检查单元格是否属于动态名称范围
【发布时间】:2017-09-12 09:53:16
【问题描述】:

我有一些动态名称范围,我想在 VBA 中检查一个单元格(例如活动单元格)是否属于其中之一。

例如,我创建了名称范围

nameTest1=OFFSET(A1;0;0;COUNT(A1:A12))

我想检查活动单元格是否在这个范围内。

我找到了各种方法来做到这一点,但不是在动态名称范围上。

有办法吗?

问候

死亡

【问题讨论】:

  • 使用Intersect 方法?不知道为什么它会与任何其他范围有所不同 - 您尝试了哪些方法?

标签: vba excel


【解决方案1】:

要进行测试,nameTest1 是一个命名范围。

If Not Application.Intersect(ActiveCell, Range("nameTest1")) Is Nothing Then
    MsgBox "activecell is in range nameTest1"
Else
    MsgBox "activecell is not in range nameTest1"
End If

【讨论】:

  • 是的,这行得通。谢谢。有没有办法检查一个单元格是否属于动态名称范围,如果是,这个范围的名称是什么?
  • For Each nr In ActiveWorkbook.Names If Not Application.Intersect(ActiveCell, Range(nr.Name)) Is Nothing Then MsgBox "activecell intersect with " & nr.Name End If Next
【解决方案2】:

要确定活动单元格属于哪个命名范围,试试这个

For Each nr In ActiveWorkbook.Names 
  If Not Application.Intersect(ActiveCell, Range(nr.Name)) Is Nothing Then 
   MsgBox "activecell intersect with " & nr.Name 
 End If 
Next 

【讨论】:

    【解决方案3】:

    我对 h2so4 的解决方案进行了一些调整:

    For Each nr In ActiveWorkbook.Names
        On Error Resume Next
        If Not Application.Intersect(ActiveCell, Range(nr.Name)) Is Nothing Then
            If Err.Number = 0 Then
                MsgBox "activecell intersect with " & nr.Name
            End If
        End If
        Err.Clear
    Next 
    

    感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多