Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.

# Solve Quadratic Equation in Excel VBA (with Quick Steps)

If you are searching for the solution or some special tricks to solve quadratic equation in Excel VBA then you have landed in the right place. There are some quick steps to solve quadratic equation in Excel VBA. This article will show you each and every step with proper illustrations so, you can easily apply them for your purpose. Let’s get into the central part of the article.

## Steps to Solve Quadratic Equation in Excel VBA

In this section, I will show you the quick and easy steps to solve quadratic equation in Excel VBA on Windows operating system. You will find detailed explanations with clear illustrations of each thing in this article. I have used Microsoft 365 version here. But you can use any other versions as of your availability. If anything of this article doesn’t work in your version then leave us a comment.

### 📌 Step 1: Assign Cells to Insert Input and Get Output

First, create a layout to take the input of co-efficient and give the output of roots. For this-

• Assign cells from C5:E5 respectively to get input of co-efficient a, b, and c of the quadratic equation.
• Then, assign cells of C10 and C11 to get the value of two roots of the quadratic equation. ### 📌 Step 2: Create VBA Module

• For this, first, go to the top ribbon and press on the Developer And then press on the Visual Basic option from the menu.
• You can use ALT + F11 to open the ‘Microsoft Visual Basic for Applications’ window if you don’t have the Developer tab added. • Now, a window named “Microsoft Visual Basic for Applications” will appear. Here from the top menu bar, press on the “Insert” And a menu will appear. From them, select the “Module’” option. • Now, a new “Module” window will appear. And Paste this VBA code into the box.
``````Sub Quadratic_Eq_VBA()
Dim a, b, c, dis, x1, x2 As Single
a = Cells(7, 3)
b = Cells(7, 4)
c = Cells(7, 5)
dis = (b ^ 2) - (4 * a * c)
If dis > 0 Then
x1 = (-b + Sqr(dis)) / (2 * a)
x2 = (-b - Sqr(dis)) / (2 * a)
Cells(10, 3) = Round(x1, 2)
Cells(11, 3) = Round(x2, 2)
ElseIf dis = 0 Then
x1 = (-b) / 2 * a
Cells(10, 3) = x1
Cells(11, 3) = x1
Else
Cells(10, 3) = "No Real Root"
End If
End Sub
``````

🔎 VBA Code Breakdown:

Segment 1:

``````Sub quadratic_Eq_VBA()
Dim a, b, c, dis, x1, x2 As Single``````

First, I have created a new Sub named Quadratic_Eq_VBA then declares 5 variables of Single format.

Segment 2:

``````a = Cells(7, 3)
b = Cells(7, 4)
c = Cells(7, 5)
dis = (b ^ 2) - (4 * a * c)``````

Then, I assigned the coefficient a, b, and c in the cells C7 to E7 respectively. Variable dis is the discriminant of the quadratic equation which is equal to the value of (b2 – 4ac).

Segment 3:

``````If dis > 0 Then
x1 = (-b + Sqr(dis)) / (2 * a)
x2 = (-b - Sqr(dis)) / (2 * a)
Cells(10, 3) = Round(x1, 2)
Cells(11, 3) = Round(x2, 2)``````

We know that when the value of the discriminant is greater than zero then there will be two distinct outputs of the quadratic equation. The formula of the roots is = -b (dis)/(2*a). Then, the Round function rounds the root value to two decimal digits. And, the first root result will be inserted in cell C10 and the second root will be inserted in cell C11.

Segment 4:

``````ElseIf dis = 0 Then
x1 = (-b) / 2 * a
Cells(10, 3) = x1
Cells(11, 3) = x1``````

And, when the value of the discriminant is equal to zero then there will be one root of the quadratic equation. And the formula will be like = – b/ (2 * a)

Segment 5:

``````Else
Cells(10, 3) = "No Real Root"
End If
End Sub``````

Lastly, if the discriminant is less than zero then, there will be no real roots. So, for this case insert “No real Root” text in cell C10. And finally, closes the If functions and ends the sub. • To run the code go to the top menu, press on the Run option, and here will open some other options and select the Run Sub/UserForm also you can simply press F5 to run the code. If there are values in the assigned cells for the co-efficient then you will get the roots of the quadratic equation in the assigned cells.

### 📌 Step 3: Create Macro Button

You can also create a macro button, so you can easily use this worksheet repeatedly. To create a macro button follow the steps below:

• First, go to the Developer tab in the top ribbon.
• Then, click on the Insert options and select the Button • After selecting the button icon, you have to draw a box in the region where you want to place the button. So draw a box in a suitable region to create a macro button. • After drawing the box, a window named “Assign macro” will appear.
• From the list, select the macro you have created before.
• Then, press OK. • As a result, you will see a macro button will be created in the selected region. Right-click on the macro button to rename ### 📌 Step 4: Insert Coefficients and Run Macro

Now, you can use this worksheet to find the roots of a quadratic equation.

• Insert the value of the coefficients in the assigned cells and click on the macro button to run the code. • As a result, you will get the real roots of the quadratic equation in the output cells. Read More: How to Solve an Equation for X When Y is Given in Excel

## Conclusion  