vba Excel UDF 未出现在下拉菜单中

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

Excel UDF not appearing in drop down menu

excelexcel-vbaexcel-2007excel-udfvba

提问by Gary's Student

I wrote a User Defined Fucntion in Excel. It works great with no issues. I even wrote a description for it under the object properties menu.

我在 Excel 中编写了一个用户定义的函数。它工作得很好,没有问题。我什至在对象属性菜单下为它写了一个描述。

The problem is, my UDF never shows up in the Excel drop down menu that appears when I start to type a function. I want the user to be able to see my UDF, named removeNumbers, when they go into a cell and start to type out a function.

问题是,我的 UDF 从未出现在我开始键入函数时出现的 Excel 下拉菜单中。我希望用户在进入单元格并开始输入函数时能够看到名为 removeNumbers 的 UDF。

I would also like them to be able to see the description which I wrote, just like the standard Excel functions.

我还希望他们能够看到我写的描述,就像标准的 Excel 函数一样。

And finally, is there a way that I can provide a description for each argument which my function takes as input?

最后,有没有一种方法可以为我的函数作为输入的每个参数提供描述?

Here is the actual code, although I don't think it will be necessary to answer my questions.

这是实际的代码,尽管我认为没有必要回答我的问题。

Function removeNumbers(sInput As String, sChoice As Boolean) As String
    Dim sSpecialChars As String
    Dim i As Long

    If (sChoice = True) Then 'if true is selected, will remove all number including 0
    sSpecialChars = "0123456789" 'This is your list of characters to be removed
    For i = 1 To Len(sSpecialChars)
        sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")

    Next
    End If

    If (sChoice = False) Then 'if false is selected, will remove all numbers excluding zero
    sSpecialChars = "123456789" 'This is your list of characters to be removed
    For i = 1 To Len(sSpecialChars)
        sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")

    Next
    End If

    removeNumbers = sInput
End Function

回答by Gary's Student

To make the function appear in the drop-down you must place it in a standard modulerather than the worksheet code area.

要使该函数出现在下拉列表中,您必须将其放置在标准模块中而不是工作表代码区域中。

回答by Chris Spicer

Another poster has already covered the need for the code to be in a standard module. With regards the argument descriptions, you should look at the MacroOptions code in this answer- although it only works in Excel 2010 or later.

另一张海报已经涵盖了将代码置于标准模块中的需求。关于参数描述,您应该查看此答案中的 MacroOptions 代码- 尽管它仅适用于 Excel 2010 或更高版本。

For Excel 2007 and earlier, the only solution I have seen is in an article by JK Pieterse. This involves using the ExecuteExcel4Macro and looks a bit complicated.

对于 Excel 2007 及更早版本,我看到的唯一解决方案是JK Pieterse一篇文章。这涉及到使用 ExecuteExcel4Macro 并且看起来有点复杂。