I’m trying to make a function to calculate a weighted sum, not counting any blank entries. (SUMPRODUCT doesn’t help, since it considers blank entries to be 0.) However, my code is running into the same issue:
Function CalcWeightedSum(values, weights) As Double
If NOT isArray(values) Then
CalcWeightedSum = values
Else
Dim total, sum
total = 0.0
sum = 0.0
Dim i
For i = LBound(values) to UBound(values)
REM the following line fails to detect empty cells
If NOT isEmpty(values(i,1)) Then
sum = sum + values(i,1) * weights(i,1)
total = total + weights(i,1)
End If
Next i
CalcWeightedSum = sum / total
End If
End Function
How can I detect these blanks, and skip over them? The above code considers any blank cell to be a 0, which is not helpful at all.
Note: I found some similar questions, and a couple of them pointed toward a function called queryEmptyCells()
. However I cannot find any documentation or examples on how this function is used. It’s not obvious, since the arguments passed into the function are simply arrays of values. If this function is indeed the solution, what does it take as arguments? What does it return? My experiments with it didn’t teach me much.
Thank you. This has been frustrating me for a few hours now.