简体   繁体   中英

Excel VBA Function Works In Some Places But Not Others

I inserted the following simple function into a Module in my spreadsheet:

Function CellName(cel As Range) As Variant
Dim nm As Name
    For Each nm In Names
        If nm.RefersTo = "=" & cel.Parent.Name & "!" & cel.Address Then
            CellName = nm.Name
            Exit Function
        End If
    Next
CellName = CVErr(xlErrNA)
End Function

I just want to fill a column with the named variables of the cells in the adjacent column.

Weird thing is, this function works in some CELLS, but in others it throws an #N/A error. In cells that do have names.

Can anyone help me understand? I'm not a VBA expert; I did do some research on this Q but found only answers that were much to more complicated questions than this.

Thanks.

to get the name wherever they are, you can use something like this:

Public Function CellName(cel As Range) As Variant
  Dim nm As Name, sht As Worksheet

  For Each nm In ThisWorkbook.Names
    If nm.RefersTo = "=" & cel.Parent.Name & "!" & cel.Address Then
      CellName = nm.Name
      Exit Function
    End If
  Next

  For Each sht In ThisWorkbook.Worksheets
    For Each nm In sht.Names
      If nm.RefersTo = "=" & cel.Parent.Name & "!" & cel.Address Then
        CellName = nm.Name
        Exit Function
      End If
    Next
  Next

  '----- skip from here if you only want single-cells

  For Each nm In ThisWorkbook.Names
    If Not Intersect(Range(nm.RefersTo), cel) Is Nothing Then
      CellName = "* " & nm.Name
      Exit Function
    End If
  Next

  For Each sht In ThisWorkbook.Worksheets
    For Each nm In sht.Names
      If Not Intersect(Range(nm.RefersTo), cel) Is Nothing Then
        CellName = "* " & nm.Name
        Exit Function
      End If
    Next
  Next

  '----- skip till here if you only want single-cells

CellName = CVErr(xlErrNA)

End Function

The second part will also show ranges which include the cell if no single-reference was found (output starts with "* " (can be deleted if there is no need)

Try changing your function to this:

Function CellName(cel As Range) As Variant
    Dim nm As Name
    For Each nm In Names
        If nm.RefersTo = "=" & cel.Parent.Name & "!" & cel.Address Or _
           nm.RefersTo = "='" & Replace(cel.Parent.Name, "'", "''") & "'!" & cel.Address Then
            CellName = nm.Name
            Exit Function
        End If
    Next
    CellName = CVErr(xlErrNA)
End Function

Some sheet names need to be enclosed in quotation marks in order for syntax rules to be preserved.

Edit: Based on a comment from David Zemens, I have updated the formula to perform a Replace(cel.Parent.Name, "'", "''") to ensure that any embedded quotation marks in the sheet name are replaced by two quotation marks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM