vba 有没有办法让 UDF 像用户单击 Fx 按钮时的本机 Excel 函数那样给出描述?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14731675/
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-08 14:54:31  来源:igfitidea点击:

Is there a way to make UDF that gives a description like the native Excel functions have when user clicks the Fx button?

excelvbaexcel-vba

提问by Dean MacGregor

If I type =vlookup((or any other native Excel function) in the formula bar and then click the little Fx button to the left of the formula I get a Function Arguments prompt with all the available arguments. In that prompt, below the functional arguments, is a one or two sentence description of the function and of each argument as you move your cursor from each argument's input box.

如果我=vlookup(在公式栏中键入(或任何其他本机 Excel 函数),然后单击公式左侧的小 Fx 按钮,我会得到一个包含所有可用参数的函数参数提示。在该提示中,在函数参数下方,当您将光标从每个参数的输入框中移动时,是对函数和每个参数的一两句描述。

When I type in the name of my UDF and click the Fx I get an input box for all of my arguments but that is it. Is there a way I can add those same helpful type of descriptions that native Excel functions have?

当我输入我的 UDF 的名称并单击 Fx 时,我会得到一个输入框,用于输入我的所有参数,但就是这样。有没有办法可以添加与本机 Excel 函数相同的有用类型的描述?

回答by JustinJDavies

Type =FormulaName(into a cell and then press Ctrl+Shift+Aand it will fill in the reference name of the arguments

输入=FormulaName(到单元格,然后按Ctrl+Shift+A,它会在自变量的引用名称填写

回答by Kendall

I suggest further investigating the Application.MacroOptions method. That method allows you to provide not only a description for the function, but also a description for each of the arguments. For example, if you have a function called "SampleFunction" that takes two arguments, you can run the following and it will set you up nicely for using the fx button with your function.:

我建议进一步研究 Application.MacroOptions 方法。该方法不仅允许您提供函数的描述,还允许您提供每个参数的描述。例如,如果您有一个名为“SampleFunction”的函数,它接受两个参数,您可以运行以下命令,它将很好地设置您在函数中使用 fx 按钮。:

Private Sub RegisterMyFunction()
    Application.MacroOptions _
        Macro:="SampleFunction", _
        Description:="calculates a result based on provided inputs", _
        Category:="My UDF Category", _
        ArgumentDescriptions:=Array( _
            "is the first argument.  tell the user what it does", _
            "is the second argument.  tell the user what it does")
End Sub

回答by Peter Albert

Yes, there is a somewhat hidden way to do that:

是的,有一种隐藏的方法可以做到这一点:

After you defined your UDF in VBA, go to the Object Browser in the Visual Basic Editor (F2). Here, in the top drop down, select VBAProject. In the window below, navigate to your UDF and right click it - select Properties:

在 VBA 中定义 UDF 后,转到 Visual Basic 编辑器 ( F2) 中的对象浏览器。在这里,在顶部下拉菜单中,选择VBAProject。在下面的窗口中,导航到您的 UDF 并右键单击它 - 选择Properties

enter image description here

在此处输入图片说明

In the properties, you can provide the description.

在属性中,您可以提供描述。

enter image description here

在此处输入图片说明

If you need further information, e.g. how to add the function to a certain category check out this OzGrid article!

如果您需要更多信息,例如如何将功能添加到某个类别,请查看这篇OzGrid 文章

回答by Dean MacGregor

EDIT

编辑

I just noted you are creating UDFs in VBA thus my reply may not be applicable to your situation.

我刚刚注意到您正在 VBA 中创建 UDF,因此我的回复可能不适用于您的情况。



Have a look at one of the sample projects in Excel SDK called Generic.

查看 Excel SDK 中名为 Generic 的示例项目之一。

It is a bare minimum skeleton to build upon

它是构建的最低限度的骨架

Right after the header include statements, you will notice a declaration of a two dimensional array where the rows represent the number of UDFs in your XLL and the columns are used for the description of the particular UDF

在标题 include 语句之后,您会注意到一个二维数组的声明,其中行代表 XLL 中 UDF 的数量,列用于描述特定 UDF

The first column is used for the name of UDF followed by the a string that contains the letters that represent the data type of each parameter in your UDF.

第一列用于 UDF 的名称,后跟包含表示 UDF 中每个参数的数据类型的字母的字符串。

These columns may be used to put description text for each of your parameters in UDFs

这些列可用于在 UDF 中放置每个参数的描述文本

The number of columns are determined by the UDF that has the largest number of parameters and the UDFs that have fewer parameters use empty strings as values that are beyond the number of parameters in such UDFs

列数由参数数量最多的UDF决定,参数较少的UDF使用空字符串作为超过此类UDF中参数数量的值

But then these descriptions will be displayed in the dialog box that pops up when your click on Fx icon

但是这些描述会在你点击 Fx 图标时弹出的对话框中显示出来

回答by Nitin

Create function like I have created below: Function CompanyNames:

创建像我在下面创建的函数:函数公司名称:

Function CompanyNames(CompanyCode As Integer, Data As Range) As String

CompanyNames = WorksheetFunction.VLookup(CompanyCode, Data, 2, False)


End Function

Run below code once and you get the argument description in Function

运行下面的代码一次,你会得到函数中的参数描述

Sub DescribeFunction()

Dim FuncName As String
Dim FuncDesc As String
Dim Category As String
Dim ArgDesc(1 To 2) As String

FuncName = "CompanyNames"
FuncDesc = "Returns the Company Name"

ArgDesc(1) = "Provide the Company Code"
ArgDesc(2) = "Select Range to lookup"


Application.MacroOptions _
  Macro:=FuncName, _
  Description:=FuncDesc, _
  Category:=Category, _
  ArgumentDescriptions:=ArgDesc
End Sub