My user defined function always returns ‘A value used in the formula is of the wrong data type’ (I am passing cell ranges to the function). I’ve searched lots of threads and am quite sure my code should work:
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
Can anyone give me any feedback please?
Try something like