Excel:从工作表调用vba函数时,将当前单元格作为parameter passing

我试图做一个VBA函数接受一个单元格作为参数,并从那里使用各种Range.Offset工作。 这个函数将在工作表单元中被调用。 为了testing,我正在使用这个简单的脚本:

Public Function testPublic(targetCell As Range) As Boolean targetCell.Offset(0, 3).Value2 = "Test is successful!" testPublic = True End Function 

要查看是否可以使单元格引用工作,我通过简单的引用,如C5,但我只获得#VALUE! 错误。 不知道这有什么问题。

试图将范围更改为变体,仍然无法正常工作

您不能在工作表中使用称为UDF的函数来更新工作表中的另一个单元格:该函数只能向包含该函数的单元格返回一个值(数组公式可以返回多个值,但只能返回到其中的单元格公式被input)。

有关更多信息,请参阅: https : //support.microsoft.com/en-us/kb/170787

尝试下面的调用代码

 If testPublic(targetCell:=Range1) Then MsgBox "success" End If 

function代码如下。 根据你的逻辑改变ByRef和ByVal。

 Public Function testPublic(ByRef targetCell As Range) As Boolean targetCell.Offset(0, 3).Value2 = "Test is successful!" testPublic = True End Function