从 VBA (Excel) 执行内置函数

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

Executing build-in functions from VBA (Excel)

excelvbaevaluate

提问by nhusnullin

I need to have a capability to execute any kind of build-in functions (such as 'sum' or 'len') from VBA (MS Excel).

我需要能够从 VBA (MS Excel) 执行任何类型的内置函数(例如“sum”或“len”)。

One of the restrictions that I have is that I may not pass cell ranges as arguments to these functions. Instead of this, I should be able to use strict values.

我的限制之一是我不能将单元格范围作为参数传递给这些函数。取而代之的是,我应该能够使用严格的值。

I want to be able to use the following expression SUM(1, 2) which should return 3, while the following version SUM("A1:A2") won't work for me.

我希望能够使用应该返回 3 的以下表达式 SUM(1, 2),而以下版本 SUM("A1:A2") 对我不起作用。

I managed to develop some function that parses my prior input and makes it consist of a list of values (as for example above, it made the user's input of 'A1:A2' look like an array of numbers consisting of two values).

我设法开发了一些函数来解析我之前的输入并使其包含一个值列表(例如上面的例子,它使用户的 'A1:A2' 输入看起来像一个由两个值组成的数字数组)。

So, can anyone give me an example of using a build-in function that receives a list of values (not just range of cells)?

那么,谁能给我一个使用内置函数接收值列表(不仅仅是单元格范围)的示例?

I tried the following code, but for some unknown reason, I haven't been able to get it working (I keep getting 1004 error, saying: Cannot run the macro 'SUM'. The macro may not be available in this workbook or all macros may be disabled.):

我尝试了以下代码,但由于某些未知原因,我无法使其正常工作(我不断收到 1004 错误,说:无法运行宏“SUM”。该宏可能在此工作簿中或全部不可用宏可能被禁用。):

Application.Run "SUM", 2, 2

Some valuable advices that would help to find a solution to this problem will be greatly appreciated.

一些有助于找到解决此问题的方法的宝贵建议将不胜感激。

回答by markblandford

To use a built-in, Excel, Worksheet function, you need to do something like the following:

要使用内置的 Excel 工作表函数,您需要执行以下操作:

Application.WorksheetFunction.Sum(2,2)