ArrayAvg – The average of an array of any type

' The average of an array of any type'' FIRST and LAST indicate which portion of the array' should be considered; they default to the first' and last element, respectively' if IGNOREEMPTY argument is True or omitted,' Empty values aren't accounted forFunction ArrayAvg(arr As Variant, Optional First As Variant, _    Optional Last As Variant, Optional IgnoreEmpty As Boolean = True) As Variant    Dim index As Long    Dim sum As Variant    Dim count As Long    If IsMissing(First) Then First = LBound(arr)    If IsMissing(Last) Then Last = UBound(arr)        ' if arr isn't an array, the following statement raises an error    For index = First To Last        If IgnoreEmpty = False Or Not IsEmpty(arr(index)) Then            sum = sum + arr(index)            count = count + 1        End If    Next        ' return the average    ArrayAvg = sum / countEnd Function

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: