2014-07-01 45 views
-1

我有一个VBA函数:如何获得当前的Excel单元格

public function MyFunction(someParameters) as Double 

    ... do something ... 

    return aValue 

end function 

我有一个在我的函数是从一些细胞称为表:

A1: =MyFunction(...) 

A2: =MyFunction(...) 

A100: =MyFunction(..) 

我想返回价值(它的工作原理),也可以添加注释到单元格...

如果我使用ActiveCell然后我使用AddComment,它添加注释到最后一个单元格离开我的游标,而不是'活动单元格计算”。

怎么办?

谢谢。

+0

您可以参考它调用UDF的细胞 - 我还没有尝试过,对于添加注释,虽然。 http://www.mrexcel.com/forum/excel-questions/379635-get-cell-reference-udf-caller.html – natancodes

回答

4

你不能这样做使用ActiveCell。你的函数重新计算,每次它被称为ActiveCell是不同的(取决于选择)。对于通过UDF添加注释的逻辑似乎有点瑕疵,但您可以尝试这个

Public Function MyFunction() As Double 

    MyFunction = 3.141519 

    Dim cell As Range 
    Set cell = Range(Application.Caller.Address) 

    cell.AddComment (cell.Address) 

End Function 
相关问题