Why does not field with custom function to get name recalculate?

隐身守侯 提交于 2019-12-24 00:15:57

问题


I use a vba function to get cell name (taken from Retrieving Cell name in Excel)

Public Function CellName(oCell As Range) As Variant
  Dim oName As Name

  For Each oName In ThisWorkbook.Names
    If oName.RefersToRange.Parent Is oCell.Parent Then
        If Not Intersect(oCell, oName.RefersToRange) Is Nothing Then
            CellName = oName.Name
            Exit Function
        End If
    End If
  Next
  CellName = CVErr(xlErrNA)
End Function

When the name of the cell does not exist, it shows error -- and that is of course intended behaviour. However, when I then name the other cell (the one which name I want to get), the error in my cell is still active. Recalculating does not help. I need then to change the value of the other cell (I can change its value or change value of yet another cell that is in its formula), or its formula, so the value in that cell would be recalculated, so my cell with CellName function gets properly refreshed.

I don't see the point why, and what can I do to simple make the cell refresh when I name the cell I point to?

This is Excel 2007, file type xlsm.


回答1:


I think you need to set the function as Volatile

at the beginning of your UDF, add this code:

application.volatile

see MSDN Library: Volatile Method [Excel 2003 VBA Language Reference]



来源:https://stackoverflow.com/questions/16604855/why-does-not-field-with-custom-function-to-get-name-recalculate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!