I am calculating values in a spreadsheet array using two variables that are the margins of the array and two variables that are fixed outside the array. Each cell calls a macro function:
=femalefitness04Small.xls!WNDf.WNDf($P$3,$P$4,$B4,C$3)
The function code is:
Option Explicit
Function WNDf(N, p, c, k)
Dim GSum, WeightedExpectedValue
Dim nm, xm As Integer
Dim s, ExpectedValue, Weight
GSum = 0
s = 1 - c
For nm = 0 To N
For xm = 0 To nm
ExpectedValue = (xm + (1 - k) * (N - nm)) / (xm + N - nm)
Weight = _
WorksheetFunction.BinomDist(nm, N, p, False) * _
WorksheetFunction.BinomDist(xm, nm, s, False)
WeightedExpectedValue = Weight * ExpectedValue
GSum = GSum + WeightedExpectedValue
Next xm
Next nm
WNDf = GSum
End Function
With the line "for nm = 0 to N", a #value error resulting from a wrong data type returns to all cells in the array. If the line is "for nm = 0 to N-1", the function returns what I think are correct values--no wrong data type. I can't figure out what changes in the first case (N, not N-1) that leads to the #VALUE. If you see what's causing the problem, I would appreciate your help. Thanks!
=femalefitness04Small.xls!WNDf.WNDf($P$3,$P$4,$B4,C$3)
The function code is:
Option Explicit
Function WNDf(N, p, c, k)
Dim GSum, WeightedExpectedValue
Dim nm, xm As Integer
Dim s, ExpectedValue, Weight
GSum = 0
s = 1 - c
For nm = 0 To N
For xm = 0 To nm
ExpectedValue = (xm + (1 - k) * (N - nm)) / (xm + N - nm)
Weight = _
WorksheetFunction.BinomDist(nm, N, p, False) * _
WorksheetFunction.BinomDist(xm, nm, s, False)
WeightedExpectedValue = Weight * ExpectedValue
GSum = GSum + WeightedExpectedValue
Next xm
Next nm
WNDf = GSum
End Function
With the line "for nm = 0 to N", a #value error resulting from a wrong data type returns to all cells in the array. If the line is "for nm = 0 to N-1", the function returns what I think are correct values--no wrong data type. I can't figure out what changes in the first case (N, not N-1) that leads to the #VALUE. If you see what's causing the problem, I would appreciate your help. Thanks!