Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB4,VB5,VB6,VBS
Expertise: beginner
Nov 20, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 for

Function 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
    ' return the average
    ArrayAvg = sum / count

End Function
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date