从传递范围获取excel工作表中的已定义名称

时间:2014-01-14 23:51:06

标签: excel delphi-2007

我正在尝试使用已定义的名称填充excel文件。我现在要做的是,一旦我搬到一个小区,例如Worksheet1!$ A $ 8,我想检索该单元格的已定义名称,告诉我所需的数据是什么。现在这是我得到的,只给我.... $ 8美元,我期望的是PROD_CATEGORY,因为这是我为该单元格定义的名称。真的很感激,如果有人可以提供帮助。

WorkSheet.Range[ColNumToAlpha(CurrCol)+IntToStr(HRow)].Name

感谢,

1 个答案:

答案 0 :(得分:1)

这是一个函数,它将返回包含传递给函数的范围的所有范围名称。例如,您可以将WorkSheet.Range[ColNumToAlpha(CurrCol)+IntToStr(HRow)]传递给此函数。

Function TellNamedRanges(ByVal Target As Range) As String
  Dim NamedRange As Name
  Dim FoundOne As Boolean
  Dim RangesFound As String

  FoundOne = False
  RangesFound = "Found these ranges: "

  For Each NamedRange In ThisWorkbook.Names
    If Not Application.Intersect(Target, Range(NamedRange.RefersTo)) Is Nothing Then
      FoundOne = True
      RangesFound = RangesFound & NamedRange.Name & " "
    End If
  Next NamedRange

  If FoundOne = False Then
    TellNamedRanges = RangesFound & "none found"
  Else
    TellNamedRanges = RangesFound
  End If
End Function