VM28
VM28

Reputation: 15

Keep getting Runtime Error '5' VBA, What is the issue?

I am creating code that can solve a quadratic equation, however I keep getting this error. It highlights the x= function every time. I don't know what is wrong with it, please help. Code is below.

Option Explicit

Sub main()
    Dim a As Double
    Dim b As Double
    Dim c As Double
    Dim x1 As Long

    a = InputBox("Write a number for a")
    b = InputBox("Write a number for b")
    c = InputBox("Write a number for c")

    x1 = (-b + Sqr((b ^ 2) - (4 * a * c))) / (2 * a)
    MsgBox (x1)

End Sub

edit: Thanks to the help I was able to run the program. However the numbers i get for x1 and x2 don't seem to make sense when trying to turn them back into the equation.

Option Explicit

Sub main()
Dim a As Double
Dim b As Double
Dim c As Double
Dim x1 As Double
Dim x2 As Double

a = InputBox("Write a number for a")
b = InputBox("Write a number for b")
c = InputBox("Write a number for c")

'If statement to check if items inside sqr are negative. Being negative would create an imaginary number, and we only need real numbers.
If (b ^ 2) - (4 * a * c) < 0 Then
  MsgBox ("The selected numbers for a, b, and c would make an imaginary number. Please try again.")
'If the selected values for abc do not create an imaginary number, the equation is run giving the two values of the x's.
Else
  x1 = (-b + Sqr((b ^ 2) - (4 * a * c))) / (2 * a)
  x2 = (b + Sqr((b ^ 2) - (4 * a * c))) / (2 * a)
  'Msgbox showing the equation with the values for abc and the values for x1 and x2.
  MsgBox (a & "(x^2)+" & b & "x+" & c & vbNewLine & "x1=" & x1 & vbNewLine & "x2=" & x2)
End If

End Sub

Edit: Never mind. I had a switched negative. Thanks.

Upvotes: 1

Views: 198

Answers (2)

braX
braX

Reputation: 11755

Include a check for imaginary numbers:

Sub main()
Dim a As Double
Dim b As Double
Dim c As Double
Dim x1 As Double

a = InputBox("Write a number for a")
b = InputBox("Write a number for b")
c = InputBox("Write a number for c")

If (b ^ 2) - (4 * a * c) < 0 Then
  MsgBox "The square root of ((" & b & " ^ 2) - (4 * " & a & " * " & c & ")) = (" & (b ^ 2) - (4 * a * c) & ") would make an imaginary number."
Else
  x1 = (-b + Sqr((b ^ 2) - (4 * a * c))) / (2 * a)
  MsgBox x1
End If

End Sub

Here is another alterative:

Using ImSqrt to return a square root of a complex number in x + yi or x + yj text format - But it can be rather "complex".

Upvotes: 2

Michał Turczyn
Michał Turczyn

Reputation: 37377

You should be more carefull and handle all cases (no soluitions, 1 and 2 solutions)

Also, should b more carefull with variable types. See code below (comments included).

Another advice: commment your code - it's harmless and can benefit greatly :)

Yet another advice: use F8 for deubgging (then you can inspect values of variables, when running the code).

Option Explicit

Sub main()
    ' All variables should be double, especially x1 and x2!
    Dim a As Double
    Dim b As Double
    Dim c As Double
    Dim x1 As Double
    Dim x2 As Double
    ' Collect data from user
    a = InputBox("Write a number for a")
    b = InputBox("Write a number for b")
    c = InputBox("Write a number for c")
    ' First step: calculate delta
    Dim delta As Double
    delta = b * b - 4 * a * c
    ' Check, if we have any solution
    If delta < 0 Then
        MsgBox "Equation has no solutions!"
        Exit Sub
    End If

    If delta = 0 Then
        x1 = -b / (2 * a)
        MsgBox "Found solution: " + CStr(x1)
        Exit Sub
    End If

    x1 = (-b + Sqr(delta)) / (2 * a)
    x2 = (-b - Sqr(delta)) / (2 * a)
    MsgBox "Found solution: x1 = " + CStr(x1) + ", x2 = " + CStr(x2)

End Sub

Upvotes: 2

Related Questions