# Free Excel Software Tool which Factors/Solves Quadratic Equation; VB Macro

This hub provides a program for math students working with the quadratic formula: ax^{2} + bx + c. The user is prompted for coefficients a, b and c, while the software factors the associated polynomial and solves for roots. This version only solves for real (non-complex) roots. Any equation (set of coefficients) can be entered. If the roots are complex, a “no solution” message will be displayed. You will need Microsoft Excel. We will go through how to install the program here. It will involve copying and pasting my code into Excel. Open up Excel, save the new file as “quadratic.xlsm”. In newer versions of Windows, you will save as type “Excel Macro-Enabled Workbook”. The workbook/spreadsheet should contain three sheets: “Sheet1”, “Sheet2”, and ‘Sheet3”. Right click on the “Sheet3” tab at bottom, and delete it. Do the same for “Sheet2”. “Sheet1” remains. It will be a spreadsheet with embedded code to solve the quadratic equation. Our program will look for the name “Sheet1”, so do not change it. You will need access to the Developers tab in Excel. To make it visible in the new spreadsheet, do as follows:

1. File --> Options (click)

2. Customize Ribbon (click)

3. Check the “Developer” box and click “OK” at bottom.

4. You will now see “Developer” tab. Click it.

5. Section dealing with macros and VBA (Visual Basic) is now visible at far left.

We will create a macro, into which we will soon paste some code. Over on the left, find and click the tab “Record Macro”. A box pops up. It has been given a default name, such as “Macro1”. Just leave it as is. There will also be a shortcut key entry, make it “ctrl a”. Click OK. ** At this point, do not press any keys or perform random mouse clicks **, or you will actually start recording keystrokes/commands in the macro. Rather

*, immediately click the “stop***, located in same place where “Record Macro” was. We have created a blank routine into which we will now place some code. Click on “Macros” tab and you will see the blank macro we just created. Click “Edit”. The macro will open up in a window, see picture in the diagram. Place the blinking cursor as shown. In order to do so, you will have to down arrow key until the cursor sits next to "End Sub". Hit Enter key 5 times to move "End Sub" down. Up arrow key 3 times to put the cursor between "End Sub" and lines above it. Now copy and paste in this code:**

*recording” tab**On Error GoTo errorhandle*

*Dim a As Single, b As Single, c As Single, root1 As Single, root2 As Single*

*Dim eqnst As String, ast As String, bst As String, cst As String*

*Dim check As Single, lenofstr As Single*

*Dim root1str As String, root2str As String*

*Sheets("Sheet1").Activate*

*Range("A1").Select*

*ActiveCell.Value = "equation"*

*ActiveCell.Offset(0, 1).Select*

*ActiveCell.Value = "root1"*

*ActiveCell.Offset(0, 1).Select*

*ActiveCell.Value = "root2"*

*ActiveCell.Offset(0, 1).Select*

*ActiveCell.Value = "factored result"*

*ActiveWindow.Zoom = 150*

*MsgBox _*

*("You will be asked for coefficients a, b and c. a cannot be zero." & _*

*" Fractions and decimals are OK. Fractions must be in 'x/y' format;" & _*

*" i.e. SINGLE DIGITS. For example, -1/2 is OK, but -10/20 is not; nor is 1/20 or 33/2."), vbOKOnly*

*ast = InputBox("enter coefficient 'a', for eqn ax^2 + bx + c." & _*

*" Include '-' sign if number is negative")*

*If Left(ast, 1) = "-" And InStr(ast, "/") <> 0 And Len(ast) > 4 Then*

*MsgBox ("Invalid fraction format entered. Needs to be x/y or -x/y.")*

*GoTo errorhandle*

*Else*

*End If*

*If Left(ast, 1) <> "-" And InStr(ast, "/") <> 0 And Len(ast) > 3 Then*

*MsgBox ("Invalid fraction format entered. Needs to be x/y or -x/y.")*

*GoTo errorhandle*

*Else*

*End If*

*If (Left(ast, 1) = "-" And Mid(ast, 3, 1) = "/") Then*

*a = Val(Mid(ast, 2, 1)) / Val(Right(ast, 1))*

*'ast = "-" + Str(a)*

*ast = ast*

*ElseIf Mid(ast, 2, 1) = "/" Then*

*a = Val(Left(ast, 1)) / Val(Right(ast, 1))*

*'ast = Str(a)*

*ast = ast*

*ElseIf ast = "1" Then*

*ast = ""*

*a = 1*

*ElseIf ast = "-1" Then*

*ast = "-"*

*a = -1*

*Else*

*a = Val(ast)*

*End If*

*bst = InputBox("enter coefficient 'b', for eqn ax^2 + bx + c." & _*

*" Include '-' sign if number is negative")*

*If Left(bst, 1) = "-" And InStr(bst, "/") <> 0 And Len(bst) > 4 Then*

*MsgBox ("Invalid fraction format entered. Needs to be x/y or -x/y.")*

*GoTo errorhandle*

*Else*

*End If*

*If Left(bst, 1) <> "-" And InStr(bst, "/") <> 0 And Len(bst) > 3 Then*

*MsgBox ("Invalid fraction format entered. Needs to be x/y or -x/y.")*

*GoTo errorhandle*

*Else*

*End If*

*If (Left(bst, 1) = "-" And Mid(bst, 3, 1) = "/") Then*

*b = Val(Mid(bst, 2, 1)) / Val(Right(bst, 1))*

*'bst = "-" + Str(b)*

*bst = bst*

*ElseIf Mid(bst, 2, 1) = "/" Then*

*b = Val(Left(bst, 1)) / Val(Right(bst, 1))*

*'bst = "+" + Str(b)*

*bst = "+" + bst*

*ElseIf bst = "1" Then*

*bst = "+"*

*b = 1*

*ElseIf bst = "-1" Then*

*bst = "-"*

*b = -1*

*ElseIf Val(bst) > 0 Then*

*b = Val(bst)*

*bst = "+" + bst*

*Else*

*b = Val(bst)*

*End If*

*cst = InputBox("enter coefficient 'c', for eqn ax^2 + bx + c." & _*

*" Include '-' sign if number is negative")*

*If Left(cst, 1) = "-" And InStr(cst, "/") <> 0 And Len(cst) > 4 Then*

*MsgBox ("Invalid fraction format entered. Needs to be x/y or -x/y.")*

*GoTo errorhandle*

*Else*

*End If*

*If Left(cst, 1) <> "-" And InStr(cst, "/") <> 0 And Len(cst) > 3 Then*

*MsgBox ("Invalid fraction format entered. Needs to be x/y or -x/y.")*

*GoTo errorhandle*

*Else*

*End If*

*If (Left(cst, 1) = "-" And Mid(cst, 3, 1) = "/") Then*

*c = Val(Mid(cst, 2, 1)) / Val(Right(cst, 1))*

*'cst = "-" + cst*

*'cst = "-" + Str(c)*

*cst = cst*

*ElseIf Mid(cst, 2, 1) = "/" Then*

*c = Val(Left(cst, 1)) / Val(Right(cst, 1))*

*cst = "+" + cst*

*ElseIf Val(cst) > 0 Then*

*c = Val(cst)*

*cst = "+" + cst*

*Else*

*c = Val(cst)*

*End If*

*check = b ^ 2 - 4 * a * c*

*If check < 0 Then*

*MsgBox "roots are complex, no real solution possible in this case.", vbOKOnly*

*eqnst = ast + "x^2" + " " + bst + "x" + " " + cst*

*Sheets("Sheet1").Activate*

*Cells.Find("equation").Select*

*Range(ActiveCell.Address).Offset(1, 0).Select*

*ActiveCell.Value = eqnst*

*ActiveCell.EntireColumn.AutoFit*

*Cells.Find("root1").Select*

*Range(ActiveCell.Address).Offset(1, 0).Select*

*ActiveCell.Value = "no real solution roots are complex"*

*ActiveCell.EntireColumn.AutoFit*

*Cells.Find("root2").Select*

*Range(ActiveCell.Address).Offset(1, 0).Select*

*ActiveCell.Value = "no real solution roots are complex"*

*ActiveCell.EntireColumn.AutoFit*

*Cells.Find("factored result").Select*

*Range(ActiveCell.Address).Offset(1, 0).Select*

*ActiveCell.Value = "N/A"*

*ActiveCell.EntireColumn.AutoFit*

*Else*

*root1 = (-b + (b ^ 2 - 4 * a * c) ^ 0.5) / (2 * a)*

*root2 = (-b - (b ^ 2 - 4 * a * c) ^ 0.5) / (2 * a)*

*eqnst = ast + "x^2" + " " + bst + "x" + " " + cst*

*Sheets("Sheet1").Activate*

*Cells.Find("equation").Select*

*Range(ActiveCell.Address).Offset(1, 0).Select*

*ActiveCell.Value = eqnst*

*ActiveCell.EntireColumn.AutoFit*

*Cells.Find("root1").Select*

*Range(ActiveCell.Address).Offset(1, 0).Select*

*ActiveCell.Value = root1*

*ActiveCell.EntireColumn.AutoFit*

*Cells.Find("root2").Select*

*Range(ActiveCell.Address).Offset(1, 0).Select*

*ActiveCell.Value = root2*

*ActiveCell.EntireColumn.AutoFit*

*Cells.Find("factored result").Select*

*Range(ActiveCell.Address).Offset(1, 0).Select*

*root1str = Str(root1)*

*root1str = Replace(root1str, " ", "")*

*lenofstr = Len(root1str)*

*If Left(root1str, 1) = "-" Then*

*root1str = "(x + " & Mid(root1str, 2, lenofstr - 1) & ")"*

*Else*

*root1str = "(x - " & root1str & ")"*

*End If*

*root2str = Str(root2)*

*root2str = Replace(root2str, " ", "")*

*lenofstr = Len(root2str)*

*If Left(root2str, 1) = "-" Then*

*root2str = "(x + " & Mid(root2str, 2, lenofstr - 1) & ")"*

*Else*

*root2str = "(x - " & root2str & ")"*

*End If*

*If a = 1 Then*

*ActiveCell.Value = root1str & root2str*

*ActiveCell.EntireColumn.AutoFit*

*ElseIf a = -1 Then*

*ActiveCell.Value = "-" & root1str & root2str*

*ActiveCell.EntireColumn.AutoFit*

*Else*

*'ActiveCell.Value = a & root1str & root2str*

*ActiveCell.Value = ast & root1str & root2str*

*ActiveCell.EntireColumn.AutoFit*

*End If*

*End If*

*If b = 0 Then*

*eqnst = ast + "x^2" + " " + cst*

*Cells.Find("equation").Select*

*Range(ActiveCell.Address).Offset(1, 0).Select*

*ActiveCell.Value = eqnst*

*ActiveCell.EntireColumn.AutoFit*

*Else*

*End If*

*If c = 0 Then*

*eqnst = ast + "x^2" + " " + bst + "x"*

*Cells.Find("equation").Select*

*Range(ActiveCell.Address).Offset(1, 0).Select*

*ActiveCell.Value = eqnst*

*ActiveCell.EntireColumn.AutoFit*

*Else*

*End If*

*Range("A1").Select*

*Exit Sub*

*errorhandle:*

*MsgBox _*

*("There was an error. Try again."), vbOKOnly*

When you paste the code in, it is likely there will be a few areas in red indicating errors in the transfer. They can be corrected (see pictures) by placing the mouse cursor * exactly flush* with the “dash” at line end and hitting “delete key”. The red type will become black, indicating it is now correct.

Close the program box and re-save the Excel workbook, keeping it open. Hit “ctrl a” and the program will run. That’s it, hope it works well for you.

## Comments

No comments yet.