如何获取当前 VBA 函数返回的工作表和单元格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10344076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:57:32  来源:igfitidea点击:

How to get worksheet and cell that the current VBA function is returning to?

excel-vbavbaexcel

提问by Jake

Is there a variable anywhere that gives the worksheet and cell that will recieve the result of a custom VBA function?

是否有任何地方提供了将接收自定义 VBA 函数结果的工作表和单元格的变量?

For example, if in A!B1the formula is =MyCustomFunc()in my code:

例如,如果A!B1公式=MyCustomFunc()在我的代码中:

public function MyCustomFunc()
    'what can I call here to get the values "A" and "B1"?
end function

采纳答案by Siddharth Rout

Is this what you are trying?

这是你正在尝试的吗?

Option Explicit

Public Function MyCustomFunc()
    '~~> Judicious use of 'Volatile' is advised.
    '~~> This will be called everytime the sheet is recalculated
    Application.Volatile

    MsgBox Application.Caller.Parent.Name & ", " & Application.Caller.Address
End Function

回答by Marten Jacobs

You want Application.ThisCell.

你要Application.ThisCell

This returns the cell which is currently being calculated.

这将返回当前正在计算的单元格。

回答by tumchaaditya

ActiveSheet.Namewill give you sheet name. ActiveCell.Rowwill give you row number and ActiveCell.Columnwill give you column letter. Then you can combine them to get cell address.

ActiveSheet.Name会给你工作表名称。ActiveCell.Row会给你行号,ActiveCell.Column会给你列字母。然后您可以将它们组合起来以获取单元格地址。