SMA Calculation

Hi Experts,

I need your help in excel file in which I calculated moving average of data. In attached I had calculated 5 data point Simple moving average using excel formula =Average(range) . I want such a formula if I change 5 to 10  in header then data give me 10 data point SMA and if I change to any number it will give me that number SMA….is that possible via formula ? if not then VBA is welcome.

See attached

Naresh PatelFinancial AdviserAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wayne Taylor (webtubbs)Commented:
You can copy this formula into cell H2 and copy down...

=AVERAGE(INDIRECT("E" & MAX(2, ROW()-H$1+1) & ":E" & ROW()))

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Naresh PatelFinancial AdviserAuthor Commented:
Naresh PatelFinancial AdviserAuthor Commented:
Will you explain how formula works? please

and is I choose 5 data point then 5th data point give me AVERAGE of 5 data point but in excel sheet it gave me results from cell H2 which must start from H6 and above all must be blank. possible ?

Wayne Taylor (webtubbs)Commented:
INDIRECT - returns a Range given a string address.
ROW - Returns the Row of the current cell, so the formula in cell H7 will return 7

The part "MAX(2, ROW()-H$1+1)" returns the row X cells up (where X = H1), to the maximum of 2. I did this so it wouldn't return an error for row < the value in cell H1. If you want those cells blank instead, use this in cell H2 and copy down.

    =IF(ROW()<=H$1, "", AVERAGE(INDIRECT("E" & ROW()-H$1+1 & ":E" & ROW())))

This will return blank if the row is less than the value in cell H1.
Naresh PatelFinancial AdviserAuthor Commented:
Thank You Very Much For Explanation and Formula is Working Fine.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.