array revisited

  • Thread starter Thread starter RobcPettit
  • Start date Start date
R

RobcPettit

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
 
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
 
Nigel, Thanks for your reply. Very good. Working on it now. Thanks for
pointing me in right direction.
Regard Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

static array 2
Moving Average of values within an array 2
averaging arrays 2
print values in an array 2
averaging non-contiguous data using arrays 1
To find Average 3
Array Memory Size 7
average of array element 11

Back
Top