2016-02-12 36 views
1

我有2個指定的區域指向同一個單元格,並且只有該單元格。我希望能夠在該單元格是目標時獲得這些範圍的名稱,但只返回其中的一個。VBA:從2個區域中的單元格獲取範圍的名稱

實施例:

命名範圍 'DATA_CELL' 是指單元格A1。 命名範圍'EVENT_CELL'是指單元格A1。

在檢測到單元格A1中的單擊時,Target.Name.Name僅返回EVENT_CELL。

如果EVENT_CELL引用多個單元,例如A1:A3,Target.Name.Name返回DATA_CELL。

有沒有人知道這是爲什麼,以及如何我可以可靠地得到這兩個範圍的名稱引用的單元格,無論任何其他單元格的範圍可能是指?

謝謝。

回答

0

這個怎麼樣,通過當前工作表,只返回每個名稱循環,作爲消息,那些與活動單元格

Sub Find_Names() 

' Loop through all names in workbook. 
For Each n In ActiveWorkbook.Names 

    ' Check to see if the name refers to the ActiveSheet. 
    If InStr(1, n.RefersTo, ActiveSheet.Name, vbTextCompare) > 0 Then 

     ' If name refers to ActiveSheet, then find the intersection of the 
     ' named range and the ActiveCell. 
     Set y = Intersect(ActiveCell, Range(n.RefersTo)) 

     ' Display a message box if the ActiveCell is in the named range. 
     If Not y Is Nothing Then MsgBox "Cell contains name : " & n.Name 

    End If 

Next 

' Display message when finished. 
MsgBox "No More Names!" 

End Sub 
+0

感謝相關的 - 我應該在我原來的職位已經說過,我正在使用工作簿中所有名稱的搜索作爲解決方法! – QAer

相關問題