2

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.

michael
  • 146
  • 3
  • 19
  • 2
    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 (`;`) – Matteo NNZ Mar 26 '15 at 16:01

1 Answers1

4

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 (;)

Community
  • 1
  • 1
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
  • 1
    Wrong, it takes the letter as well. – Matteo NNZ Mar 26 '15 at 15:59
  • Cells() can use a column's string name. I didn't think it would work either when I saw someone else using it on this board until I tried it. I'm using 2007 for reference. – Sobigen Mar 26 '15 at 16:00
  • 1
    @MatteoNNZ The IntelliSense calls the parameters `[RowIndex]` and `[ColumnIndex]`... damn Microsoft! – Mathieu Guindon Mar 26 '15 at 16:02
  • 3
    Moreover, the function crashes on the beginning, it doesn't enter the code. I guess he should separate values by semi-columns (`;`) and not commas (`,`), it should be related to the operating system – Matteo NNZ Mar 26 '15 at 16:02
  • Exactly, damn Microsoft :) – Matteo NNZ Mar 26 '15 at 16:02
  • Mat, you can report the comment on your answer body to make it the correct one ;) – Matteo NNZ Mar 26 '15 at 16:05
  • 1
    The problem was the comma. The correct usage of function require the semicolumn on my Excel 2011 for Mac, Italian version. MatteoNNZ you win! – michael Mar 26 '15 at 16:11