我的用户定义函数总是返回“公式中使用的值的数据类型错误”(我将单元格区域传递给函数)。我搜索了很多线程并且很确定我的代码应该可以工作:
Function SortAndEvaluate(ByRef Probs() As Variant, ByRef ResidualProbs() As Variant, ByRef Costs() As Variant)
Dim Temp As Double
Dim i As Integer
Dim NoExcanges As Integer
'Exchange values in probs in descending order
Do
NoExchanges = True
' Loop through each element
For i = 0 To UBound(Probs) - 1
'If element is greater than the last exchange, else do nothing
If Probs(i) > Probs(i + 1) Then
NoExchanges = False
' Exchange probability values
Temp = Probs(i)
Probs(i) = Probs(i + 1)
Probs(i + 1) = Temp
' Exchange residual probability values
Temp = ResidualProbs(i)
ResidualProbs(i) = ResidualProbs(i + 1)
ResidualProbs(i + 1) = Temp
' Exchange cost values
Temp = Costs(i)
Costs(i) = Costs(i + 1)
Costs(i + 1) = Temp
End If
Next i
Loop While Not (NoExchanges)
Temp = 0
For i = 1 To UBound(Probs)
If i = 1 Then
Temp = Temp + Probs(i) * Costs(i)
Else
Temp = Temp + Probs(i) * ResidualProbs(i - 1) * Costs(i)
End If
Next i
SortAndEvaluate = Temp
End Function
任何人都可以给我任何反馈吗?