A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
The function works correctly when you call it from VBA, but when you use it as a worksheet function, FindNext immediately returns Nothing although there are further matches.
The following version works in a worksheet formula, but you have to specify the range:
Public Function calcGrade(rng As Range) As Double
Dim graded As New Collection
Dim credits As Integer
Dim grade As Double
Dim cumCredits As Integer
Dim cumGrade As Double
Dim finalGrade As Double
Dim Match As Range
For Each Match In rng
If Match.Value = True Then
credits = Match.Offset(1, 0).Value
grade = Match.Offset(2, 0).Value
If grade <> 0 Then
graded.Add (credits * grade)
cumCredits = cumCredits + credits
End If
End If
Next Match
Dim val As Variant
For Each val In graded
cumGrade = cumGrade + val
Next val
finalGrade = cumGrade / cumCredits
'Debug.Print finalGrade
calcGrade = finalGrade
End Function
Example of use:
=calcGrade(B2:K10)