我正在编写一个简单的二等分代码以查明方程的根。为此,我将公式输入到Excel的VBA中,然后运行该公式以查找不匹配错误。错误在“ MyFunc =“
”行弹出我已经检查了语法,并且看起来正确。被调用的值和括号都包含相同的变量。
Function MyFunc(T)
Dim a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3,
a4, b4, c4, d4, n1, n2, n3, n4 As Double
Sheets("Sheet1").Select
Range("E4").Select
a1 = ActiveCell.Value
Range("F4").Select
b1 = ActiveCell.Value
Range("G4").Select
c1 = ActiveCell.Value
Range("H4").Select
d1 = ActiveCell.Value
Range("E5").Select
a2 = ActiveCell.Value
Range("F5").Select
b2 = ActiveCell.Value
Range("G5").Select
c2 = ActiveCell.Value
Range("H5").Select
d2 = ActiveCell.Value
Range("E6").Select
a3 = ActiveCell.Value
Range("F6").Select
b3 = ActiveCell.Value
Range("G6").Select
c3 = ActiveCell.Value
Range("H6").Select
d3 = ActiveCell.Value
Range("E7").Select
a4 = ActiveCell.Value
Range("F7").Select
b4 = ActiveCell.Value
Range("G7").Select
c4 = ActiveCell.Value
Range("H7").Select
d4 = ActiveCell.Value
Range("C5").Select
n1 = ActiveCell.Value
Range("C6").Select
n2 = ActiveCell.Value
Range("C7").Select
n3 = ActiveCell.Value
Range("C8").Select
n4 = ActiveCell.Value
MyFunc = -2635500 + (((n1 * (a1 * (T - 298.15)) +
(1 / 2 * b1 * (T - 298.15) ^ 2) + (1 / 3 * c1 * (T - 298.15) ^ 3) +
(1 / 4 * d1 * (T - 298.15) ^ 4))) + (n2 * (a2 * (T - 298.15)) +
(1 / 2 * b2 * (T - 298.15) ^ 2) + (1 / 3 * c2(T - 298.15) ^ 3) +
(1 / 4 * d2 * (T - 298.15) ^ 4)) + (n3 * (a3 * (T - 298.15)) +
(1 / 2 * b3 * (T - 298.15) ^ 2) + (1 / 3 * c3 * (T - 298.15) ^ 3) +
(1 / 4 * d3 * (T - 298.15) ^ 4)) + (n4 * (a4 * (T - 298.15)) +
(1 / 2 * b4 * (T - 298.15) ^ 2) + (1 / 3 * c4 * (T - 298.15) ^ 3) +
(1 / 4 * d4 * (T - 298.15) ^ 4)))
End Function
这只是一个存储值T的公式,该值将用于二等分代码中,以找到以下等式的根。
答案 0 :(得分:-1)
Function MyFunc(T)
Dim a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3, _
a4, b4, c4, d4, n1, n2, n3, n4 As Double
Sheets("Sheet1").Select
a1 = Range("E4").Value
b1 = Range("F4").Value
c1 = Range("G4").Value
d1 = Range("H4").Value
a2 = Range("E5").Value
b2 = Range("F5").Value
c2 = Range("G5").Value
d2 = Range("H5").Value
a3 = Range("E6").Value
b3 = Range("F6").Value
c3 = Range("G6").Value
d3 = Range("H6").Value
a4 = Range("E7").Value
b4 = Range("F7").Value
c4 = Range("G7").Value
d4 = Range("H7").Value
n1 = Range("C5").Value
n2 = Range("C6").Value
n3 = Range("C7").Value
n4 = Range("C8").Value
MyFunc = -2635500 + (((n1 * (a1 * (T - 298.15)) + _
(1 / 2 * b1 * (T - 298.15) ^ 2) + (1 / 3 * c1 * (T - 298.15) ^ 3) + _
(1 / 4 * d1 * (T - 298.15) ^ 4))) + (n2 * (a2 * (T - 298.15)) + _
(1 / 2 * b2 * (T - 298.15) ^ 2) + (1 / 3 * c2(T - 298.15) ^ 3) + _
(1 / 4 * d2 * (T - 298.15) ^ 4)) + (n3 * (a3 * (T - 298.15)) + _
(1 / 2 * b3 * (T - 298.15) ^ 2) + (1 / 3 * c3 * (T - 298.15) ^ 3) + _
(1 / 4 * d3 * (T - 298.15) ^ 4)) + (n4 * (a4 * (T - 298.15)) + _
(1 / 2 * b4 * (T - 298.15) ^ 2) + (1 / 3 * c4 * (T - 298.15) ^ 3) + _
(1 / 4 * d4 * (T - 298.15) ^ 4)))
End Function
您的Dim
语句仅将n4分配为双精度。所有其他变量均为变量。如果将参数定义为函数定义的一部分,则可以输入更灵活的值:
Public Function MyFunc2(a1 As Double, b1 As Double, c1 As Double, d1 As Double, _
a2 As Double, b2 As Double, c2 As Double, d2 As Double, _
a3 As Double, b3 As Double, c3 As Double, d3 As Double, _
a4 As Double, b4 As Double, c4 As Double, d4 As Double, _
n1 As Double, n2 As Double, n3 As Double, n4 As Double, _
T As Double)
MyFunc2 = -2635500 + (((n1 * (a1 * (T - 298.15)) + _
(1 / 2 * b1 * (T - 298.15) ^ 2) + (1 / 3 * c1 * (T - 298.15) ^ 3) + _
(1 / 4 * d1 * (T - 298.15) ^ 4))) + (n2 * (a2 * (T - 298.15)) + _
(1 / 2 * b2 * (T - 298.15) ^ 2) + (1 / 3 * c2(T - 298.15) ^ 3) + _
(1 / 4 * d2 * (T - 298.15) ^ 4)) + (n3 * (a3 * (T - 298.15)) + _
(1 / 2 * b3 * (T - 298.15) ^ 2) + (1 / 3 * c3 * (T - 298.15) ^ 3) + _
(1 / 4 * d3 * (T - 298.15) ^ 4)) + (n4 * (a4 * (T - 298.15)) + _
(1 / 2 * b4 * (T - 298.15) ^ 2) + (1 / 3 * c4 * (T - 298.15) ^ 3) + _
(1 / 4 * d4 * (T - 298.15) ^ 4)))
End Function
在工作表中,定义公式:=myFunc2(E4, F4, G4, ...)