I have a very basic problem, but somehow just don't find the solution to it. I tried to write a little program, which takes the given stock prices from a worksheet and calculates the moving average of it over a given period of time.
So far that was not a problem.
My problem now is, that I want the solution i.e. the moving average in a new worksheet in column B and the equivalent stockprices in column A.
I already wrote the code to create a new worksheet as follows:
Sub CopyToNewSheet()
Selection.Copy
Sheets.Add(After:=Sheets(1)).Name = "MovingAverage"
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
This works fine so far. The next part of the program should now be the calculation of the moving average. I tried this, which worked as well:
Sub CalculateMA(maLength As Integer)
Application.ScreenUpdating = False
Columns("B:B").Delete
Range("B1").Value = "MA" + CStr(maLength)
Range("B1").Offset(maLength, 0).Select
Do While IsEmpty(Selection.Offset(0, -1)) = False
Selection.Value = WorksheetFunction.Average(Range(Selection.Offset(-maLength + 1, -1), Selection.Offset(0, -1)))
Selection.Offset(1, 0).Select
Loop
End Sub
The problem here is that this does not put the calculated solution into the new worksheet, but into the old one.
Do you have any ideas, what I have to change, so that the solution is posted into the other worksheet?
Thanks a lot.
Your help is appreciated.