Search code examples
vbaexcelexcel-udf

excel function syntax calling error


I've created this function:

Public Function getLastCellValue(ByVal row As Integer, ByVal column As String) As String
If (Cells(row, column).Value = "") Then
    getLastCellValue = getLastCellValue(row - 1, column)
Else: getLastCellValue = Cells.Item(row, column).Value
End If
End Function

When I use it in a cell this:

=getLastCellValue(1,"C")  

Excel tells me that the function contains an error and focuses on the second parameter: "C".

I'm going crazy because I do not understand the mistake.


Solution

  • Cells(row, column) expects numeric parameter values. If you mean to refer to cell addresses (like, "A1") then it might be simpler to use Range instead.

    That said I strongly recommend taking this implementation over to Code Review once you get it to work as expected, ...I'd have several things to point out ;)


    Your code works as is on my machine. My bet is on @Matteo's comment

    Make sure your Excel language is using the comma (,) to separate functions parameters; if your name tells me something about your nationality, I guess you're using an Italian/Spanish system (which means you should separate inputs by semi-column (;)