Have lots of equations to solve? It's easy and can be done at lightening speed.
First, add a Command button on your worksheet: on the Developer tab, in the Controls group, click Insert, and then under ActiveX Controls, click Command Button. Next, click the worksheet location at which you want the upper-left corner of the command button to appear and adjust its size and properties, so it looks similar to this presented here:
Click View Code in the Controls group. This launches the Visual Basic Editor. Add the following VBA macro code in the window:
Sub SolveQuadraticEquation()
'QuadraticEquationSolver
Dim a, b, c, det, root1, root2 As Single
On Error GoTo ErrHandler
coef = InputBox("Enter a b c coefficientss separated by slash(/)", "Quadratic Equation Solver", "1.5/-2/3")
a = Left(coef, Application.WorksheetFunction.Search("/", coef, 1) - 1)
b = Left(Right(coef, Len(coef) - Application.WorksheetFunction.Search("/", coef, 1)), Application.WorksheetFunction.Search("/", Right(coef, Len(coef) - Application.WorksheetFunction.Search("/", coef, 1)), 1) - 1)
c = Right(coef, Len(coef) - Len(a) - Len(b) - 2)
det = (b ^ 2) - (4 * a * c)
If det > 0 Then
root1 = (-b + Sqr(det)) / (2 * a)
root2 = (-b - Sqr(det)) / (2 * a)
outp = MsgBox("Root1: " & root1 & " Root2: " & root2, , "Roots of the equation")
ElseIf det = 0 Then
root1 = (-b) / 2 * a
outp = MsgBox("Root1: " & root1 & " Root2: " & root1, , "Roots of the equation")
Else
outp = MsgBox("NO ROOT", , "Roots of the equation")
End If
ErrHandler:
End Sub
'QuadraticEquationSolver
Dim a, b, c, det, root1, root2 As Single
On Error GoTo ErrHandler
coef = InputBox("Enter a b c coefficientss separated by slash(/)", "Quadratic Equation Solver", "1.5/-2/3")
a = Left(coef, Application.WorksheetFunction.Search("/", coef, 1) - 1)
b = Left(Right(coef, Len(coef) - Application.WorksheetFunction.Search("/", coef, 1)), Application.WorksheetFunction.Search("/", Right(coef, Len(coef) - Application.WorksheetFunction.Search("/", coef, 1)), 1) - 1)
c = Right(coef, Len(coef) - Len(a) - Len(b) - 2)
det = (b ^ 2) - (4 * a * c)
If det > 0 Then
root1 = (-b + Sqr(det)) / (2 * a)
root2 = (-b - Sqr(det)) / (2 * a)
outp = MsgBox("Root1: " & root1 & " Root2: " & root2, , "Roots of the equation")
ElseIf det = 0 Then
root1 = (-b) / 2 * a
outp = MsgBox("Root1: " & root1 & " Root2: " & root1, , "Roots of the equation")
Else
outp = MsgBox("NO ROOT", , "Roots of the equation")
End If
ErrHandler:
End Sub
Close the Visual Basic Editor, and click Design Mode to ensure design mode is off.When you click the button, the macro will run and show the following window:
Enter the three coefficients of your equation, separated by slash and click OK button. The result will be displayed immediately.