我希望编写一个用户自定义函数(在工作簿的模块中定义),它可以接受一系列单元格并为下一个单元格生成值。它看起来像这样:
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
如果我在单元格A2中使用公式=doStuff(A1),我希望A2=1,A3="doStuff"。但是每次执行到nextCell.Value = "doStuff"
这一行时,总是出现错误"Application-defined or object-defined error"
。我是否操作正确或者有解决方法?谢谢。