Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88066

WorksheetFunction.Average returns incorrect answer

$
0
0

Given the data in B2:D10 I wanted to find the average in each row use VBA array formula. Following a book example VBA Queue by Bill Jelen.

Worksheet data

Name    A   B   C   Answer  Manual Avg
A2      4   34  59  31.5    32.33333333
A3      45  83  74  59.5    67.33333333
A4      98  40  17  57.5    51.66666667
A5      95  8   83  89  62
A6      57  55  98  77.5    70
A7      14  21  17  15.5    17.33333333
A8      55  23  4   29.5    27.33333333
A9      80  15  77  78.5    57.33333333
A10     11  24  40  25.5    25

Answer in Column E is from this formula

Sub QuickFillAverage()
Dim myArray As Variant
Dim myCount As Integer

myArray = Worksheets("Sheet1").Range("B2:D10")

For myCount = LBound(myArray) To UBound(myArray)
    Worksheets("Sheet1").Cells(myCount + 1, 5).Value = _
    WorksheetFunction.Average(myArray(myCount, 1), myArray(myCount, 3))
Next myCount

End Sub

The answer in Column F is from

(B2+C2+D2) / 3

Done for each row to find the average. As you can see the answers are different. Why?


Viewing all articles
Browse latest Browse all 88066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>