x
とy
の値を計算するマクロを作成しました。これらの値をExcel上のセルに書き込もうとしています。セルに値を出力しようとすると#VALUEエラーが発生するVBA
セルに値を表示しようとすると、#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
@ Cominternありがとうございます。ワークシートに計算された値を表示するための代替提案はありますか?私はボタンを追加し、そのボタンに機能を割り当てようとしました。しかし、オプションのエラーではありません。 – Dazzler
[関数からセル値を設定する]の複製があります(http://stackoverflow.com/questions/15659779/set-a-cell-value-from-a-function) –