Thread: Average
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bernie Deitrick Bernie Deitrick is offline
external usenet poster
 
Posts: 5,441
Default Average

Rob,

By default, arrays are 0-based, and your variable j is initially zero, so
you are actually working with 15 5's and 1 zero.

Either use

Dim arr(1 to 15) As Single

or put

Option Base 1

at the top of your module.

Below is a fixed version...

HTH,
Bernie
MS Excel MVP

Sub NewSub()
Dim j As Integer
Dim x As Variant
Dim k As Variant

Dim arr(1 To 15) As Single

For x = 1 To 15
arr(x) = 5
Next x

j = Application.WorksheetFunction.Average(arr)
k = Application.WorksheetFunction.StDev(arr)
MsgBox j & " " & k

End Sub


"RobcPettit" wrote in message
oups.com...
Hi Im trying to figure out why:
Dim j As Variant, x As Variant, k as variant
Dim arr(15) As Single

For x = 1 To 15
arr(j) = 5
j = j + 1
Next x

j = Application.WorksheetFunction.Average(arr)
k = Application.WorksheetFunction.StDev(arr),
does not return the expected results of 5 and 0. I get 4.68 and 1.25.
Any Ideas. Regards Robert