在 Excel VBA 代码中使用 Excel 公式函数(ERF、ERFC)?

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

Using Excel Formula functions (ERF, ERFC) in Excel VBA code?

excelvbaexcel-vba

提问by TheTreeMan

I'm trying to use the error function and the complimentary error function in my program. Neither are working. I keep getting the error Compile Error: Sub or Function not defined. However, if I go into a cell and manually try to use the error function, it works.

我正在尝试在我的程序中使用错误函数和免费错误函数。两者都没有工作。我不断收到错误Compile Error: Sub or Function not defined。但是,如果我进入一个单元格并手动尝试使用错误函数,它会起作用。

Why is that? How can I use it in my actual code?

这是为什么?如何在我的实际代码中使用它?

The error function is ERF(x)and the complimentary error function is ERFC(x).

误差函数为ERF(x),互补误差函数为ERFC(x)

Here's an example of things that don't work:

下面是一个不起作用的例子:

Sub SeriouslyWHYIsntThisWorking()
    x = 3
    Range("A1") = Erf(x)
End Sub

Even this doesn't work:

即使这不起作用:

Sub PleaseWork()
    Range("A1") = Erfc(1)
End Sub

But if I went into Excel and typed =ERF(3)or =ERFC(1)into a cell, it'll work.

但是如果我进入 Excel 并在单元格中输入=ERF(3)=ERFC(1)输入,它就会起作用。

I'm very new to this and probably missing something incredibly simple. Help would be greatly appreciated!

我对此很陌生,可能缺少一些非常简单的东西。帮助将不胜感激!

采纳答案by SeanC

To use a worksheet formula in vba, you need to put Application.WorksheetFunction.in front of it.

要在vba中使用工作表公式,需要Application.WorksheetFunction.在它前面加上。

Some functions do have vba equivalents, but (as far as I know) not in the case of erfand erfc

有些功能确实有VBA等价物,但(据我所知)没有的情况下,erferfc

回答by Gaffi

Do you have the Analysis Toolpak for VBA add-in installed/referenced? (Look for atpvbaen.xls)

您是否安装/引用了用于 VBA 加载项的分析工具库?(寻找 atpvbaen.xls)

The ERFfunction is part of that add-in, and there are two versions of it (one for use in Excel functions, and one for VBA), and you'll need the right one set up and referenced by your project to be usable.

ERF函数是该加载项的一部分,它有两个版本(一个用于 Excel 函数,一个用于 VBA),您需要正确设置并由您的项目引用才能使用。

The add-ins are standard from MSFT, but not always set up by default. If you can use it in Excel normally, then you've already set up at least the Excel version. So using it all like it looks like you want to do will require the add-in, regardless of how you implement/use that function. Meaning, if you want to share this with anyone else, they will need the add-in installed/activated.

加载项是 MSFT 的标准配置,但并非总是默认设置。如果你能在Excel中正常使用,那么你至少已经设置了Excel版本。因此,无论您如何实现/使用该功能,都将需要加载项,就像您想要的那样使用它。意思是,如果你想与其他人分享这个,他们需要安装/激活加载项。



To link together this answer with the others provided (which are equally accurate and correct), either setting a cell value with

要将这个答案与提供的其他答案(同样准确和正确)链接在一起,或者设置一个单元格值

Range("A1").value = Application.WorksheetFunction.ERF(x)

Range("A1").value = Application.WorksheetFunction.ERF(x)

or setting a cell formula with

或设置单元格公式

Range("A1").Formula = "=Erfc(" + x + ")"

Range("A1").Formula = "=Erfc(" + x + ")"

will require the end-user using the add-in.

将需要最终用户使用加载项。

回答by Thomas Kelley

Try this:

尝试这个:

Sub ThisShouldWorkNow()
    x = 3
    formula = "=Erfc(" + x + ")"
    Range("A1").Formula = formula
End Sub

Totally untested, since I don't have Excel on my Linux machine... But I think I'm getting the point across -- you need to use the .Formula property of the Range object.

完全未经测试,因为我的 Linux 机器上没有 Excel ......但我想我明白了——你需要使用 Range 对象的 .Formula 属性。

There's more information here:

这里有更多信息:

http://msdn.microsoft.com/en-us/library/office/gg192736.aspx

http://msdn.microsoft.com/en-us/library/office/gg192736.aspx