#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 108
Default Average

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Average

Because the array has 16 elements not 15.
For x = 0 To 15 works.

An array starts on zero unless you have set it to 1 in an Option Base
Statement. See help for more info on this.

Regards,

Ossiemac

"RobcPettit" wrote:

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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 108
Default Average

Thankyou both for your replys. Once I added the option base that
solved my problem. Thankyou for the explanation, this is something I
can be aware of in the future.
Regards Robert

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
A formula to AVERAGE IF but only average a set number of values [email protected] Excel Worksheet Functions 2 January 31st 08 08:28 PM
Find monthly average but have average automatically configured kimbafred Excel Discussion (Misc queries) 2 August 8th 07 12:28 AM
Error Handling #N/A with AVERAGE Function - Average of values in Row Sam via OfficeKB.com Excel Worksheet Functions 13 July 31st 05 03:59 PM
Weighed Average of a weiged average when there are blanks krl - ExcelForums.com Excel Discussion (Misc queries) 1 July 6th 05 07:37 PM
how does one convert text to a formula "average(A:A)" to =average( phshirk Excel Worksheet Functions 4 April 14th 05 01:20 AM


All times are GMT +1. The time now is 06:08 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"