Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 80
Default array revisited

Hi, Sorry to ask again. Im asigning values to a vba static array, when
I got 100 values, is there a way to calculate the average of the last
50 values, then if I add further values, Iwant to average only the last
50 added.
Regards Robert

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 923
Default array revisited

You can determine the average by looping through the Array and computing the
arithmetical mean....

For x = 1 to 50
xSum = xSum + myArray(x)
Next x
xMean = xSum / 50

(above assumes Option Base = 1)

The question posed, to calculate a moving average, depends on the ability to
track the last value in the array. The array has 100 values, any or all of
which could be set to zero. You could scan the array looking for the first
empty value and then use that to compute the mean of the preceding 50
values.

Sub aTest()

Dim x As Integer, LastEntry As Integer
Dim xSum As Double
Dim xSpan As Integer

xSpan = 50 ' sets the number of values to compute the mean over

' determine the last non-empty value in the array
For x = 1 To 100
If IsEmpty(myArray(x)) Then
LastEntry = x - 1
Exit For
End If
Next x

' compute the mean for the last 50 ( xSpan) of values in array
If LastEntry = xSpan Then
For x = LastEntry To LastEntry - xSpan + 1 Step -1
xSum = xSum + myArray(x)
Next x
MsgBox "Mean: " & xSum / xSpan
Else
MsgBox "Insufficient entries to compute"
End If

End Sub



--
Cheers
Nigel



wrote in message
oups.com...
Hi, Sorry to ask again. Im asigning values to a vba static array, when
I got 100 values, is there a way to calculate the average of the last
50 values, then if I add further values, Iwant to average only the last
50 added.
Regards Robert



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 80
Default array revisited

Nigel, Thanks for your reply. Very good. Working on it now. Thanks for
pointing me in right direction.
Regard 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
14 Day Average REVISITED F. Lawrence Kulchar Excel Discussion (Misc queries) 4 September 8th 08 11:54 PM
Help with averages revisited TimJames Excel Worksheet Functions 6 March 10th 08 12:20 PM
EXCEL Stability Revisited DRA Excel Discussion (Misc queries) 7 November 8th 07 02:08 PM
Last row, last column revisited David O. Antillon Excel Programming 4 August 2nd 05 04:08 AM
Revisited: If Cell in row has data then do this: HELP Yogi_Bear_79 Excel Programming 4 February 4th 04 04:49 PM


All times are GMT +1. The time now is 03:25 PM.

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"