VBA、Excel 中的平方根函数

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

Square root function in VBA, Excel

vbaexcel-vbaexcel

提问by Bathsheba

What is the square root function in VBA excel? I tried SQRT() and Root() but none of them are working, my Excel version in 2013.

VBA excel中的平方根函数是什么?我尝试了 SQRT() 和 Root() 但它们都不起作用,我的 Excel 版本是 2013 年。

回答by Bathsheba

Use the oddly named

使用奇怪的名字

VBA.Sqr(n)

VBA.Sqr(n)

where nis your number for which you want the square root computed. The VBA.prefix is optional, but it helps descriminate between VBAand any other library you might have referenced.

n你想要计算平方根的数字在哪里。该VBA.前缀是可选的,但它有助于之间descriminateVBA和任何其他库,你可能会被引用。

回答by Robin Mackenzie

Nth rootcan be obtained with VBA with:

可以使用 VBA 获得第 N 个根

Option Explicit

Function NthRoot(dblNumber As Double, lngRoot As Long) As Double
    NthRoot = dblNumber ^ (1 / lngRoot)
End Function

So for square root:

所以对于平方根:

? NthRoot(64, 2)
 8 
? NthRoot(64.33333, 2)
 8.02080606921773 

回答by barrowc

You can use the ^ operatorto obtain square roots. Just raise the number you started with to the power of 0.5:

您可以使用^ 运算符来获得平方根。只需将您开始的数字提高到 0.5 的幂:

81^0.5will give you 9 and 256^0.5will give you 16

81^0.5会给你 9256^0.5会给你 16