user7581677
user7581677

Reputation:

Square root function in VBA, Excel

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

Answers (4)

Arpan Saini
Arpan Saini

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

barrowc
barrowc

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

Robin Mackenzie
Robin Mackenzie

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

Bathsheba
Bathsheba

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

Related Questions