0% found this document useful (0 votes)
56 views

CMT400 Excercise 3 (Excel VBA) : The Quadratic Solver: A Ac B B A Ac B B

This document provides instructions for creating a quadratic equation solver in Excel using VBA. The solver will accept coefficients a, b, and c input by the user in cells. It will then use the quadratic formula to calculate the roots based on the discriminant and output the results. The solver is created by adding a button, assigning it a macro that declares variables to store the coefficients and roots, calculates the discriminant, and applies the appropriate case of the quadratic formula to display the roots in designated cells.

Uploaded by

Alina Aziz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

CMT400 Excercise 3 (Excel VBA) : The Quadratic Solver: A Ac B B A Ac B B

This document provides instructions for creating a quadratic equation solver in Excel using VBA. The solver will accept coefficients a, b, and c input by the user in cells. It will then use the quadratic formula to calculate the roots based on the discriminant and output the results. The solver is created by adding a button, assigning it a macro that declares variables to store the coefficients and roots, calculates the discriminant, and applies the appropriate case of the quadratic formula to display the roots in designated cells.

Uploaded by

Alina Aziz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

CMT400 Excercise 3 (Excel VBA): The Quadratic Solver

You will solve a quadratic equation using worksheet and also userform.
Excercise 3a: The Quadratic Solver( Using Worksheet)
In this exercise, the worksheet will be used as the host application. A button
control will be added in the worksheet and Visual basic codes will be written for
that control. The codes will appear as module in the Visual basic editor.
Please refer to lecture notes for details of the formula used.
Brief summary of the equations are given here.
Case 1: det >0
Root1 = ( b b 2 4ac ) / 2a
Root2 = (b b 2 4ac ) / 2a
Case 2: det =0

Root2 = Root1 = b / 2a
Case 3: det <0
No root
Examples of chemistry problems that requires solving quadratic equation are
a. Chemical equilibrium
b. Acid-base equilibrium i.e calculation of pH of a weak acid or weak base
Procedure
1. Open MS Excel
2. Save the file as QuadraticSolver.xls
3. Type in cells A2, A3 and A4 : a, b and c respectively. Type root 1 and root
2 in the following two cells below i.e A5 and A6 as shown in Fig. 1 below:
4. You do not have to copy the examples of the quadratic functions. These
are just for your reference. You can check your answers later with these
answers.
5. Insert Button(Form Control) by selecting Developer tab > Insert. Choose
from the Form controls not from ActiveX control.
6. Place the button in a suitable place..a macro dialog box appears as in Fig.
2.
7. Change the macro name to QuadSolver and click new. The Visual Basic
editor will automatically open as a module. By default it wll give the name
of the button in increasing order..the name can also be changed if
necessary.

Fig. 1

Fig. 2.
8. Copy the codes below into the module.
Sub Button1_Click()
Dim a, b, c, det, root1, root2 As Single
a = Cells(2, 2)
b = Cells(3, 2)
c = Cells(4, 2)
det = (b ^ 2) - (4 * a * c)
If det > 0 Then
root1 = (-b + Sqr(det)) / (2 * a)

root2 = (-b - Sqr(det)) / (2 * a)


'Cells(5, 2) = Round(root1, 2)
'Cells(6, 2) = Round(root2, 2)
Cells(5, 2) = root1
Cells(6, 2) = root2
ElseIf det = 0 Then
root1 = (-b) / 2 * a
Cells(5, 2) = root1
Cells(6, 2) = root1
Else
Cells(5, 2) = "No root"
Cells(6, 2) = "No root"
End If
End Sub
9. Rename the button as Quadratic equation Solver.
10.Test the solver by putting in values of a b and c in cells B2:B4. The two
roots will be displayed in cells B5 and B6.

You might also like