I am very new to excel-vba programming and I was learning how to write a custom vba function using worksheet function.
If I go to a cell and type in the following formula, I will get the cell name.
=ADDRESS(ROW(), COLUMN(), 4)
E.g., If we enter the above formula into cell “A6”, the cell will return the value =
Now I am trying to write a vba function to do the exact same thing as following:
Function GetThisCellName() As String GetThisCellName= WorksheetFunction.Address(WorksheetFunction.Row(), WorksheetFunction.Column(), 4) End Function
However when I am trying to use this function inside a cell as follows:
the cell will return
#VALUE!. What might be wrong with my code?
If you want to return address of cell, from which formula was called, use Application.ThisCell:
Function GetThisCellName() As String GetThisCellName = Application.ThisCell.Address(False, False) End Function
Column are not members of WorksheetFunction object. You can use this syntax to get the address of the cell:
Function GetThisCellName() As String GetThisCellName = ActiveCell.Address(False, False, xlA1) End Function
See also Range.Address property