Upload
alina-aziz
View
2
Download
1
Embed Size (px)
DESCRIPTION
qwerty
Citation preview
CMT400 Excercise 3 (Excel VBA): The Quadratic SolverYou 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 =
Root2 =
Case 2: det =0
Root2 = Root1 =
Case 3: det 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.
_1397913416.unknown
_1397913448.unknown