【问题标题】:#VALUE error when trying to output value to a cell excel VBA尝试将值输出到单元格excel VBA时出现#VALUE错误
【发布时间】:2016-12-09 09:53:49
【问题描述】:

我编写了一个计算 xy 值的宏。我在尝试将这些值写入 Excel 上的单元格时遇到问题。

当我尝试在单元格上显示值时出现#VALUE 错误。

我在下面添加了我的代码。关于代码有什么问题的任何建议都会非常有帮助和赞赏?

提前致谢!

'Compute Points
Function ComputePoints(x1, y1, x2, y2, distance) As Double

'Calculate slope m
Dim m As Double
m = (y2 - y1) / (x2 - x1)

'Calculate intercept
Dim Intercept As Double
Intercept = y1 - m * x1

'Calculate x for distFinal
Dim message As String
Dim a As Double
Dim b As Double
Dim c As Double
Dim root1 As Double
Dim root2 As Double
Dim det As Double
Dim det1 As Double

Dim wb As Workbook
Dim ws As Worksheet
Dim x1Rng As Range
Dim x2Rng As Range
Dim yRng As Range

a = (m ^ 2 + 1)
b = 2 * (Intercept * m - m * y2 - x2)
c = x2 ^ 2 + (Intercept - y2) ^ 2 - distance ^ 2

det = ((b ^ 2) - (4 * a * c))

det1 = Sqr(det)

message = "There is no solution to your equation"

If det < 0 Then
    MsgBox message, vbOKOnly, "Error"
 Else
    root1 = Round((-b + det1) / (2 * a), 2)
    root2 = Round((-b - det1) / (2 * a), 2)
 End If

'Compute y
Dim y As Double
y = m * root2 + Intercept

' Trying to set cell values to root1, root2, y
Set wb = ActiveWorkbook
Set ws = wb.Sheets("Sheet9")

Set x1Rng = ws.Range("N2")
Set x2Rng = ws.Range("O2")
Set yRng = ws.Range("P2")

x1Rng.Value2 = root1
x2Rng.Value2 = root2
yRng.Value2 = y

ComputePoints = y

End Function

【问题讨论】:

  • 从工作表调用的函数不能更改其他单元格的值。
  • @Comintern 谢谢。任何替代建议来显示在工作表上计算的值?我尝试添加一个按钮并将功能分配给该按钮。但是我遇到了 Argument not optional 错误。
  • @Dazzler 很明显 - 您需要指定参数值。编写一个无参数的Sub 过程,使用它需要的任何参数调用该函数,然后从该按钮调用Sub 过程。为每个参数指定一个显式类型也没有什么坏处,它们也不需要传递ByRefPrivate Function ComputePoints(ByVal x1 As Double, ByVal y1 As Double, ByVal x2 As Double, ByVal y2 As Double, ByVal distance As Double) As Double 应该这样做。
  • @Mat'sMug - 谢谢你的建议。会试一试:)

标签: vba excel


【解决方案1】:

我稍微修改了您的代码以直接在 excel 单元格中获取值。您需要选择 3 个水平单元格,按 F2 / =,输入您的公式,然后按 Ctrl Shift 输入使其成为array formula

这将为您提供单元格中的三个输出值。

Function ComputePoints(x1, y1, x2, y2, distance)

    Dim results(3) As Variant ' @nightcrawler23

    'Calculate slope m
    Dim m As Double
    m = (y2 - y1) / (x2 - x1)

    'Calculate intercept
    Dim Intercept As Double
    Intercept = y1 - m * x1

    'Calculate x for distFinal
    Dim message As String
    Dim a As Double
    Dim b As Double
    Dim c As Double
    Dim root1 As Double
    Dim root2 As Double
    Dim det As Double
    Dim det1 As Double

    a = (m ^ 2 + 1)
    b = 2 * (Intercept * m - m * y2 - x2)
    c = x2 ^ 2 + (Intercept - y2) ^ 2 - distance ^ 2

    det = ((b ^ 2) - (4 * a * c))

    det1 = Sqr(det)

    message = "There is no solution to your equation"

    If det < 0 Then
        MsgBox message, vbOKOnly, "Error"
     Else
        root1 = Round((-b + det1) / (2 * a), 2)
        root2 = Round((-b - det1) / (2 * a), 2)
     End If

    'Compute y
    Dim y As Double
    y = m * root2 + Intercept

    results(1) = root1    ' @nightcrawler23
    results(2) = root2    ' @nightcrawler23
    results(3) = y        ' @nightcrawler23

    ComputePoints = results    ' @nightcrawler23

End Function

当找不到根时,您需要添加一些代码以输出错误消息

【讨论】:

  • 感谢您的回答。但我在最后一行 ComputePoints = results 收到类型不匹配错误
  • 你的原始代码有Function ComputePoints(x1, y1, x2, y2, distance) As Double。我在我的代码中改变了这一点。你有吗?
  • 抱歉错过了那个。修复了问题!!谢谢你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 1970-01-01
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多