Frage

I'm trying to populate an excel file using defined names. What I like to do now is that once I move to a cell e.g. Worksheet1!$A$8, I want to retrieve the defined name for that cell which tells me what is the data needed. Right now this is what I got and only give me ....$A$8, what I expect is PROD_CATEGORY as this is what I defined the name to be for that cell. Really appreciate if anyone can help.

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

thanks,

War es hilfreich?

Lösung

Here's a function that will return all the range names containing the range you pass to the function. You could, for instance, pass WorkSheet.Range[ColNumToAlpha(CurrCol)+IntToStr(HRow)] to this function.

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top