How to do square root in VBA?
A lot of you reached out with this question on how to do square root in Excel VBA.
It is a fairly simple formula. Even the VBA syntax is manageable.
Let`s jump in and see what we can do!
To get to square root of a number in an Excel formula
We need to use the SQR function.
It can be found on the ribbon under the formulas tab.
You can find it in the Math/Trig formulas.
Syntax of the SQR function
For a formula you can use this syntax
The number you want to get the square root of must be positive.
If you put a negative number within the brackets it will throw an error.
Luckiy we can use the ABS formula to correct this. If you put =ABS(SQR(number)) then the possible negative number will get converted into the absolute value which will be positive.
Sub test Dim myResult As Double myResult = Sqr(300) Msgbox myResult End Sub
Syntax for VBA
The syntax for VBA is pretty much the same as for the formula.
In the VBA editor you can put:
The result will be: 33
You can see the proper use within a Subroutine on the left.
In this example the Variant myResult will be written out in to a messagebox, and it will contain the result of 100.
Come and visit us.
Come and visit excelangel on Facebook to find and join the growing community of true office workers.
Oh, I almost forgot.
Drop us a like while you there…