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.


Download Practice Workbook

You can download the practice workbook from here:


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.

Solve Quadratic Equation in Excel VBA

Read More: How to Solve For x in Excel (2 Simple Ways)


📌 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.

Microsoft Visual Basic for Applications

  • 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.

Inserting Module

  • 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.

Microsoft Visual Basic for Applications - VBA Code

  • 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.

Run VBA 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.


Similar Readings


📌 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

Insert a Macro 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.

Create 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.

Assign Macro

  • As a result, you will see a macro button will be created in the selected region. Right-click on the macro button to rename

Solve Quadratic Equation in Excel VBA


📌 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.

Insert Coefficients and Run Macro

  • As a result, you will get the real roots of the quadratic equation in the output cells.

Solve Quadratic Equation in Excel VBA

Read More: How to Solve an Equation for X When Y is Given in Excel


Conclusion

In this article, you have found how to solve quadratic equations in Excel VBA. I hope you found this article helpful. You can visit our website ExcelDemy to learn more Excel-related content. Please, drop comments, suggestions, or queries if you have any in the comment section below.


Related Articles

Osman Goni Ridwan

Osman Goni Ridwan

I am Ridwan, graduated from Naval Architecture and Marine Engineering Dept, BUET, currently residing in Dhaka, Bangladesh. And my passion is to grow up my skillsets with industry demands. My prime goal is to be a data analyst as I do love to solve problems and play with data.

We will be happy to hear your thoughts

Leave a reply

ExcelDemy
Logo