Reputation:
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.
Upvotes: 4
Views: 32137
Reputation: 5219
Sqr : is the function used to calcualte the sqare root, or you can use ^0.5
Function getDistance(x1, y1, x2, y2)
getDistance = Sqr((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
End Function
Upvotes: 0
Reputation: 10679
You can use the ^ operator to obtain square roots. Just raise the number you started with to the power of 0.5:
81^0.5
will give you 9 and 256^0.5
will give you 16
Upvotes: 0
Reputation: 19289
Nth root can be obtained with VBA with:
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
Upvotes: 1
Reputation: 234805
Use the oddly named
VBA.Sqr(n)
where n
is your number for which you want the square root computed. The VBA.
prefix is optional, but it helps descriminate between VBA
and any other library you might have referenced.
Upvotes: 11