I want to write a user-defined function (defined in a workbook's module) which takes a range of cells and generate value for the next cell. It looks like this:
Function doStuff(ByRef input As Range) As Integer
Dim columnCount As Integer
Dim nextCell As Range
columnCount = input.Columns.Count
' nextCell is 2 cells away on the right of the last cell on the first row of input
Set nextCell = input.Cells(1, columnCount).Offset(0, 2)
nextCell.Value = "doStuff"
doStuff = columnCount
End Function
In cell A2, if I use the formula =doStuff(A1), I expect A2=1 and A3="doStuff". Somehow I always got the error "Application-defined or object-defined error"
when it got to the nextCell.Value = "doStuff"
line. I'm I doing it correctly or is there a workaround this? Thank you.