問題描述
使用活動單元格查找範圍名稱 (Find Range Name Using Active Cell)
我是 VBA 編程的初學者。
我有一個單元格是命名範圍的一部分。使用該活動單元格,我希望能夠找到該單元格所屬的範圍,並將其作為 Range 對像傳遞給 VBA 函數或子例程。
任何人都可以為我提供有關如何操作的指導繼續,或者這不可能?
提前致謝!
參考解法
方法 1:
Here is a simple example.
The code checks if the selected cell is part of a named range. If so, the named range is passed to a function:
Sub Main()
Dim nm As Integer
For nm = 1 To ActiveWorkbook.Names.Count
If Not Intersect(Selection, Range(ActiveWorkbook.Names(nm).Name)) Is Nothing Then
Debug.Print MyFunc(Range(ActiveWorkbook.Names(nm).Name)) // Prints TRUE or FALSE
End If
Next nm
End Sub
Function MyFunc(Named_Range As Range) As Boolean
MyFunc = Named_Range.Cells.Count > 2 ~~>Courtesy of `Thomas Inzina`
End Function