当使用一些定义的变量进行简单除法问题时,我在UDF中遇到#value错误。变量都定义为双精度。
提前道歉,因为我是一名机械工程师,所以我的编码非常业余。我有一本我在excel中使用的设计书,该书最终可能会长很多列,因此为避免这种情况,我正在尝试编写一些UDF,以消除仅用于其他单元格计算的所有单元格。这样一来,我开始尝试对变量进行除法运算时就遇到了一个问题。
在代码中,直到Cx变暗我才遇到问题。我记下了从SFA_Radians开始的所有内容,并将函数设置为输出每个变量。在达到Cx之前,一切都是好的和期望值。无论我做什么,似乎都被变量除以#VALUE错误。非常感谢您的帮助。
Function RADEFFECTSF(SFA As Range, FacetAngle As Range, FacetRadius As Range) As Variant
Dim Slope1 As Double
Dim Slope2 As Double
Dim X1 As Double
Dim Y1 As Double
Dim X2 As Double
Dim Y2 As Double
Dim b1 As Double
Dim b2 As Double
Dim SFA_Radians As Double
Dim FacetAngle_Radians As Double
Dim Pi As Double
Dim Cx As Double
Dim Cy As Double
Dim CalcArray() As Double
Dim i As Long
Pi = Application.WorksheetFunction.Pi()
ReDim CalcArray(1 To SFA.Cells.Count)
For i = 1 To SFA.Cells.Count
SFA_Radians = SFA(i) * Pi / 180
FacetAngle_Radians = FacetAngle(i) * Pi / 180
Slope1 = -Tan(SFA_Radians)
Slope2 = Tan(FacetAngle_Radians)
X1 = FacetRadius(i) * Sin(SFA_Radians)
Y1 = FacetRadius(i) * Cos(SFA_Radians)
b1 = Y1 - (Slope1 * X1)
X2 = -FacetRadius(i) * Sin(FacetAngle_Radians)
Y2 = FacetRadius(i) * Cos(FacetAngle_Radians)
b2 = Y2 - (Slope2 * X2)
Cx = (b2 - b1) / (Slope1 - Slope2)
Cy = (Slope1 * (Cx / (b1))) + b1
CalcArray(i) = Cy - (FacetRadius(i) * Cos(SFA_Radians))
Next i
RADEFFECTSF = CalcArray()
End Function
答案 0 :(得分:0)
我已经在一些示例数据上尝试了您的代码,并且可以正常工作:
根据我的经验(我也是工程师),很可能您在范围内导入的某些数据不是数字。检查一下。
Slope1 - Slope2
的值可以为0,因此请放置一个条件。对b1
的评论相同。
逐行检查您在另一个范围内获得的值,以准确查看失败的地方,只保留其余代码作为注释。
Function RADEFFECTSF(SFA As Range, FacetAngle As Range, FacetRadius As Range) As Variant
...
Pi = 4 * Atn(1)
ReDim CalcArray(1 To SFA.Cells.Count)
For i = 1 To SFA.Cells.Count
SFA_Radians = SFA(i) * Pi / 180
FacetAngle_Radians = FacetAngle(i) * Pi / 180
Slope1 = -Tan(SFA_Radians)
Slope2 = Tan(FacetAngle_Radians)
X1 = FacetRadius(i) * Sin(SFA_Radians)
CalcArray(i) = X1 'This line is just to return the data X1
'Y1 = FacetRadius(i) * Cos(SFA_Radians)
'b1 = Y1 - (Slope1 * X1)
'X2 = -FacetRadius(i) * Sin(FacetAngle_Radians)
'Y2 = FacetRadius(i) * Cos(FacetAngle_Radians)
'b2 = Y2 - (Slope2 * X2)
'If Slope1 <> Slope2 then
'Cx = (b2 - b1) / (Slope1 - Slope2)
'Else
'Place what it is expected when Slope1=Slope2
'End If
'Cy = (Slope1 * (Cx / (b1))) + b1
'CalcArray(i) = Cy - (FacetRadius(i) * Cos(SFA_Radians))
Next i
RADEFFECTSF = CalcArray()
End Function
检查X1是否为预期的……等等。然后,您可以找到引入意外值的代码行。
抱歉,请给我一个答复,但我仍然无法发表评论。