我不确定这是否是一个优雅的解决方案,但它是一个简单的解决方案。
下面的代码假定范围名称在 Sheet2 的单元格 A1、A2、A3 等中,并且列表以空白单元格终止。它还假设 B、C 等列中没有任何内容。您必须根据实际情况调整代码。
Sub GetNameDetails()
Dim Inx As Integer
Dim NameCrnt As String
Dim Pos As Integer
Dim RangeCrnt As String
Dim RowCrnt As Integer
RowCrnt = 1
With Sheets("Sheet2")
Do While True
' This loop is repeated for every cell in column A until it
' encounters a blank cell
NameCrnt = .Cells(RowCrnt, 1).Value
If NameCrnt = "" Then Exit Do
For Inx = 1 To Names.Count
' This matches the names in Sheet 2 with the named ranges.
' Names that cannot be found in the Names collection are ignored.
If Names(Inx).Name = NameCrnt Then
RangeCrnt = Names(Inx).RefersTo ' Extract full address of range
RangeCrnt = Mid(RangeCrnt, 2) ' Discard =
RangeCrnt = Replace(RangeCrnt, "$", "") ' Remove $s
Pos = InStr(RangeCrnt, "!")
' Save sheet name
.Cells(RowCrnt, 2).Value = Mid(RangeCrnt, 1, Pos - 1)
RangeCrnt = Mid(RangeCrnt, Pos + 1) ' Discard sheet name
.Cells(RowCrnt, 3).Value = RangeCrnt ' Save full address of range
Pos = InStr(RangeCrnt, ":")
If Pos <> 0 Then
RangeCrnt = Mid(RangeCrnt, 1, Pos - 1) ' Discard end of range if any
End If
.Cells(RowCrnt, 4).Value = .Range(RangeCrnt).Row
.Cells(RowCrnt, 5).Value = .Range(RangeCrnt).Column
Exit For
End If
Next
RowCrnt = RowCrnt + 1
Loop
End With
End Sub
结果是一个包含五列的表:
Col 1 = Range name (unchanged)
Col 2 = Sheet name
Col 3 = Range
Col 4 = Top row of range
Col 5 = Left column of range
按第 4 列和第 5 列排序后,表格将按您查找的顺序排列。