不断收到运行时错误'5'VBA,这是什么问题?

VM28

我正在创建可以解决二次方程式的代码,但是我不断收到此错误。每次高亮显示x =函数。我不知道这是怎么回事,请帮忙。代码如下。

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

编辑:由于我的帮助,我得以运行该程序。但是,当我将x1和x2转换为方程式时,我得到的数字似乎没有意义。

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

编辑:没关系。我有一个负面的开关。谢谢。

布拉克斯

包括对虚数的检查:

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

这是另一个替代方案:

使用ImSqrt以文本x + yix + yj文本格式返回复数的平方根-但是它可能相当“复杂”。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我收到运行时错误NZEC请告诉我是什么问题

来自分类Dev

JAVA:为什么我会不断收到这些运行时错误?

来自分类Dev

我的VBA代码有什么问题(我收到运行时错误9)

来自分类Dev

VBA 帮助 - 运行时错误 5:无效的过程调用或参数,仅在首次运行时

来自分类Dev

我有这个问题。我不断收到运行时错误,因为它同时打印2行?

来自分类Dev

我在VBA excel中编写编码器。我不断收到运行时错误13。它说不匹配

来自分类Dev

无效的过程调用或参数vba。运行时错误5

来自分类Dev

无效的过程调用或参数vba。运行时错误5

来自分类Dev

VBA 宏返回运行时错误“5”

来自分类Dev

带有 listobjects.add 的 Excel 2013 VBA 运行时错误 5

来自分类Dev

删除 SQL 连接时出现 VBA 运行时错误 5

来自分类Dev

VBA 数据透视表运行时错误 5:无效的过程调用或参数

来自分类Dev

MySQL Workbench 6.0.8.11354(6.0)中出现错误“索引超出范围”,这是什么问题?

来自分类Dev

我的ID不为null,为什么会收到运行时错误?

来自分类Dev

收到运行时错误1004,我不明白为什么

来自分类Dev

为什么我在 Excel VB 中收到运行时错误 429?

来自分类Dev

为什么我在 Xcode 上收到以下代码的 (lldb) 运行时错误?

来自分类Dev

在SPOJ上获取运行时错误(SIGSEGV),无法找出我的代码有什么问题

来自分类Dev

在SPOJ上获取运行时错误(SIGSEGV),无法找出我的代码有什么问题

来自分类Dev

VBA:运行时错误“ 91”?

来自分类Dev

Excel VBA运行时错误1004

来自分类Dev

VBA宏上的运行时错误

来自分类Dev

运行时错误400 VBA

来自分类Dev

运行时错误2471访问VBA

来自分类Dev

Excel VBA运行时错误1004

来自分类Dev

VBA宏上的运行时错误

来自分类Dev

运行时错误VBS / VBA

来自分类Dev

运行时错误13 VBA Excel

来自分类Dev

VBA - 运行时错误“1004”

Related 相关文章

  1. 1

    我收到运行时错误NZEC请告诉我是什么问题

  2. 2

    JAVA:为什么我会不断收到这些运行时错误?

  3. 3

    我的VBA代码有什么问题(我收到运行时错误9)

  4. 4

    VBA 帮助 - 运行时错误 5:无效的过程调用或参数,仅在首次运行时

  5. 5

    我有这个问题。我不断收到运行时错误,因为它同时打印2行?

  6. 6

    我在VBA excel中编写编码器。我不断收到运行时错误13。它说不匹配

  7. 7

    无效的过程调用或参数vba。运行时错误5

  8. 8

    无效的过程调用或参数vba。运行时错误5

  9. 9

    VBA 宏返回运行时错误“5”

  10. 10

    带有 listobjects.add 的 Excel 2013 VBA 运行时错误 5

  11. 11

    删除 SQL 连接时出现 VBA 运行时错误 5

  12. 12

    VBA 数据透视表运行时错误 5:无效的过程调用或参数

  13. 13

    MySQL Workbench 6.0.8.11354(6.0)中出现错误“索引超出范围”,这是什么问题?

  14. 14

    我的ID不为null,为什么会收到运行时错误?

  15. 15

    收到运行时错误1004,我不明白为什么

  16. 16

    为什么我在 Excel VB 中收到运行时错误 429?

  17. 17

    为什么我在 Xcode 上收到以下代码的 (lldb) 运行时错误?

  18. 18

    在SPOJ上获取运行时错误(SIGSEGV),无法找出我的代码有什么问题

  19. 19

    在SPOJ上获取运行时错误(SIGSEGV),无法找出我的代码有什么问题

  20. 20

    VBA:运行时错误“ 91”?

  21. 21

    Excel VBA运行时错误1004

  22. 22

    VBA宏上的运行时错误

  23. 23

    运行时错误400 VBA

  24. 24

    运行时错误2471访问VBA

  25. 25

    Excel VBA运行时错误1004

  26. 26

    VBA宏上的运行时错误

  27. 27

    运行时错误VBS / VBA

  28. 28

    运行时错误13 VBA Excel

  29. 29

    VBA - 运行时错误“1004”

热门标签

归档