EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Math & Trig Functions VBA Square Root
Secondary Sidebar
VBA Math & Trig Functions
  • VBA Math & Trig
    • VBA Random Number
    • VBA Number Format
    • VBA Integer
    • VBA MOD
    • VBA IsNumeric
    • VBA Round
    • VBA INT
    • VBA RoundUp
    • VBA Intersect
    • VBA Randomize
    • VBA Square Root

VBA Square Root

By Madhuri ThakurMadhuri Thakur

VBA Square Root

Excel VBA Square Root Function

In this article, we will see an outline on Excel VBA Square Root. Every time working on Excel, you must have used the SQRT function that gives the square root of any whole number as a result. Is it possible to capture the square root of a number under VBA? Well, the answer is a sure Yes! You can get the square root of any positive number through VBA using the VBA SQR function. This function is a built-in VBA function and can be used on any positive integer to get the square root of it. Well, this function is very similar to the one which we use as a spreadsheet function SQRT and also take the same single argument “Number” which should be positive. We can’t get the square roots of Imaginary numbers either and entering negative values is of no use because there is no possible way with which a negative square value of a number can be captured.

Syntax:

Syntax for VBA square root function is pretty simple and is look similar to the one of spreadsheet SQRT function.

VBA Square Root Syntax

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Where Number is a sole argument that needs to be provided and should be strictly positive. Since we can’t have a negative square values of any number, chances of getting a negative square root value is impossible. Also, no scope for finding out the square roots of the complex numbers. This argument can be provided directly as a number or a variable assigned to the number or a spreadsheet cell reference where the number is stored. Also, the point to be noted here is, system by default considers the argument you provide as a double to give you the square root on any number irrespective of the fact that it is a whole square or not (Ex. 87, is not the whole square, but will still get the square root value since the number type is double. You can still define a number as an integer and then use it as an argument to SQR function. It will round to the closest square root value if the number provided is not a whole square.

How to Use Square Root Function in Excel VBA?

Below are the different examples to use the Square Root function in Excel VBA.

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (86,641 ratings)
You can download this VBA Square Root Excel Template here – VBA Square Root Excel Template

VBA Square Root – Example #1

Follow the steps below to see how exactly the VBA SQR function works.

Step 1: Click on the Developer tab and then click the Visual Basic (ALT +F11) icon.

Developer Tab

Step 2: Open Module from the Insert menu tab as shown below.

Insert Module

Step 3: Define a new sub-procedure that can hold the macro under the newly inserted module.

Code:

Sub sqrt_Example1()

End Sub

VBA Square Root Example 1-3

Step 4: Use the assignment operator to assign the value of the VBA square root function to a variable named “sqr_root” so that we can print the result and see if it works fine.

Code:

Sub sqrt_Example1()

sqr_root = Sqr(121)

End Sub

VBA Square Root Example 1-4

Step 5: Use the MsgBox function so that we can print the result as a message box that will pop-up as soon as we run the code.

Code:

Sub sqrt_Example1()

sqr_root = Sqr(121)

MsgBox "Square Root of Given Number is: " & sqr_root

End Sub

VBA Square Root Example 1-5

Step 6: Run this code by hitting F5 or Run button placed at the uppermost panel.

VBA Environ Example2-10

As soon as you run this code, you should see an output as shown below.

Message Box Example 1-6

VBA Square Root – Example #2

Let’s dive deeper into the VBA SQR function and try to find out some different cases.

Step 1: Define sub-procedure that can hold your macros.

Code:

Sub sqrt_Example2()

End Sub

VBA Square Root Example 2-1

Step 2: We need to define two new variables as integer. One for storing the value which we want a square root for. Another variable would be for storing the result of the square root after we apply the SQR function.

Code:

Sub sqrt_Example2()

Dim square_num As Integer
Dim square_root As Integer

End Sub

VBA Square Root Example 2-2

Step 3: Assign value as 87 to the square_num variable using the assignment operator (‘=’). This is the number for which we wanted to capture the square root value.

Code:

Sub sqrt_Example2()

Dim square_num As Integer
Dim square_root As Integer
square_num = 87

End Sub

VBA Square Root Example 2-3

Step 4: Use the SQR function and assignment operator to assign the value of the square root to the  square_root variable. Use square_num as an argument under the SQR function (the number for which we wanted the square root value).

Code:

Sub sqrt_Example2()

Dim square_num As Integer
Dim square_root As Integer
square_num = 87
square_root = Sqr(square_num)

End Sub

VBA Square Root Example 2-4

Step 5: Use MsgBox Property to show the value of the square root as a message that pops up as soon as you run the code.

Code:

Sub sqrt_Example2()

Dim square_num As Integer
Dim square_root As Integer
square_num = 87
square_root = Sqr(square_num)
MsgBox "Square Root for Given Number is: " & square_root

End Sub

VBA Square Root Example 2-5

Step 6: Run this code by hitting the F5 or Run button which is placed on the topmost ribbon of VBE.

Run button Example

Step 7: See the output in a message box as shown below:

VBA Square Root Example 2-6

However, this is not the actual square root value for number 87. This is the square root value for number 81. Why in the world VBA is giving wrong output for SQR function?

Well, there is nothing wrong with VBA. If something is wrong, it is ideal with our code.

Since we have defined the variables as integers, we are restricting the VBA to give the square roots of only those numbers which are whole square numbers (Ex. 4, 9, 16, 25, etc.). If any number is not a whole square, the VBA SQR function instead of throwing an error, it gives a square root value for a number that is close to the current number. Here, 87 has 81 as the closest whole square number. Therefore, you are getting the output as 9.

Step 8: Let’s see what happens when we tweak the code by defining the square_root variable as Double instead of Integer.

Code:

Sub sqrt_Example2()

Dim square_num As Integer
Dim square_root As Double
square_num = 87
square_root = Sqr(square_num)
MsgBox "Square Root for Given Number is: " & square_root

End Sub

variables as integers Example 2-7

Step 9: You will see that the system now can capture the actual square root value for the number 87.

variables as integers Example 2-8

Things to Remember

  • If you provide a negative number as an argument under the VBA SQR function, it will throw a Run-time error ‘5’. Since it is not possible to have a number with negative square values.
  • If you have a negative number, make sure you are converting it to a positive entity with the help of the ABS function. You can also check if the number is positive or not with the help of the SGN function under VBA.
  • If you put zero as an argument under the VBA SQR function, you will get the square root value as zero.

Recommended Articles

This is a guide to VBA Square Root. Here we discuss how to use Square Root Function in Excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA IF Statements
  2. VBA Sort
  3. VBA While Loop
  4. VBA Counter
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more