Excecl VBA Classic Excel VBA 2010 Excel VBA 365 Excel VBA Examples About Us

Quadratic Equation Solver


Quadratic equation is a fairly straight forward high school mathematics problem. The quadratic equation solver was programmed to determine the number of roots the equation has as well as to compute the roots. It uses the determinant b2 -4ac to solve the problems.

If b2 -4ac>0, then it has two roots and if b2 -4ac=0, then it has one root, else it has no root.

To obtain the roots, the program uses the standard quadratic formula :

To create the quadratic equation solver, start an Excel Worksheet and design the interface, as shown below:

The Design UI

The Code

Private Sub Cmd_Compute_Click()
Dim a, b, c, det As Integer
Dim root1, root2 As Single
a = Cells(8, 4)
b = Cells(9, 4)
c = Cells(10, 4)
det = (b ^ 2) - (4 * a * c)

If det > 0 Then

Cells(11, 4) = 2
root1 = (-b + Sqr(det)) / (2 * a)
root2 = (-b - Sqr(det)) / (2 * a)
Cells(12, 4) = Round(root1, 4)
Cells(12, 5) = Round(root2, 4)

ElseIf det = 0 Then
root1 = (-b) / 2 * a
Cells(11, 4) = 1

Cells(12, 4) = Round(root1, 4)
Cells(12, 5) = ""
Else
Cells(11, 4) = 0
Cells(12, 4) = "No Root"
Cells(12, 5) = ""
End If
End Sub
The Runtime UI





Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved   [Privacy Policy]

Contact: Facebook Page