Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Moving Average of values within an array

What is the best way to calculate moving averages within VBA? I have an
array of values: MyArray(1 to 1000, 1 to 1)
I would like to fill MyOtherArray(1 to 1000, 1 to 1) with 50-value moving
averages of the values in MyArray.
So for x=1 to 49, MyOtherArray(x,1) would be equal to #N/A, because there
are not yet 50 values to average
For x=50, MyOtherArray(50,1) would be equal to the average of the first 50
values of MyArray.

If I were calculating moving averages in a worksheet, I would take the
average of 50 cell ranges using the offset function. What would be a good
way to do this within VBA?

Thanks!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Moving Average of values within an array

SumArray = 0
For i = 1 to 1000
SumArray = myArray(i,1) + SumArray
if i< 50 then
MyOtherArray(i,1) = "N/A"
elseif i 50 then
SumArray = SumArray - myArray(i-50,1)
MyOtherArray(i,1) = SumArray/50
end if
next i

I think that's what you want. It ain't elegant, but I think it works.

"Eric Johnson" wrote:

What is the best way to calculate moving averages within VBA? I have an
array of values: MyArray(1 to 1000, 1 to 1)
I would like to fill MyOtherArray(1 to 1000, 1 to 1) with 50-value moving
averages of the values in MyArray.
So for x=1 to 49, MyOtherArray(x,1) would be equal to #N/A, because there
are not yet 50 values to average
For x=50, MyOtherArray(50,1) would be equal to the average of the first 50
values of MyArray.

If I were calculating moving averages in a worksheet, I would take the
average of 50 cell ranges using the offset function. What would be a good
way to do this within VBA?

Thanks!

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
Average of absolute values of moving ranges Amy Excel Discussion (Misc queries) 1 February 22nd 10 04:31 PM
Moving average billregal Excel Worksheet Functions 1 January 23rd 09 01:39 AM
Prevent cell/array references from changing when altering/moving thecell/array nme Excel Discussion (Misc queries) 1 September 19th 08 01:53 PM
meaning of : IF(Switch; Average(array A, array B); array A) DXAT Excel Worksheet Functions 1 October 24th 06 06:11 PM
Moving average Hunterbo Charts and Charting in Excel 0 August 3rd 05 09:13 PM


All times are GMT +1. The time now is 08:59 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"