
- 1 (59.48 KiB) Viewed 68 times
Trying to write a code in Excel VBA to solve a quadratic
equation with real, repeated, and complex roots. It is not working.
Can anyone figure out why? I get a mismatch error for x2 in the
complex roots and an overflow error for the repeated roots.
G1 X fx Values5 A B C D E F G 1 Quadratic Equation Solver 2 3 Coefficients Values 4 a 1 5 b 4 Solve 6 с 5 Values Reset 7 8 Roots 9x1 10 x2 11 12 13 14 15 16 17 18
(General) Sub Button1_Click() Dim a, b, c, det, x1, x2 As Single = a = Cells(4, 2) b = Cells(5, 2) C = Cells(6, 2) det = (b ^ 2) - (4 * a * c) = * If det > 0 Then x1 =

-b + Sqr(det)) / (2 * a) x2 = - (-b - Sqr(det)) / (2 * a) Cells(9, 2) = Round(x1, 2) Cells(10, 2) = Round(x2, 2) * = = Elself det = 0 Then x1 = (-b) / (2 * a) Cells(9, 2) = Round(x1, 2) Cells(10, 2) Round(x1, 2) = * * Else x1 = -(-b) / (2 * a) & "-" & Sqr(det * -1) / (2 * a) & ";" x2 = (-b) / (2 * a) & "+" & Sqr(det * -1) / (2 * a) & "i" Cells(9, 2) = x1 Cells(10, 2) x2 = = End If End Sub Sub Button2_Click() Range("b4:b10").ClearContents End Sub