Range select macro - Trendline (Moving Average)

Wayne01

New Member
Joined
May 27, 2009
Messages
38
Hi all

I have a macro that applies a trendline (moving average type) to my data series. The moving average period is specified by the user from a popup InputBox (see code below).

Code:
Sub Add_Red_Trendlines()
Dim TrendNum
TrendNum = InputBox(Prompt:="Insert Moving Average Period Below:", _
Title:="MOVING AVERAGE PERIOD", Default:="INSERT NUMBERS ONLY")
If TrendNum = "INSERT NUMBERS ONLY" Or _
TrendNum = "" Or _
TrendNum = vbaCancel Then
Exit Sub
End If
If Not IsNumeric(TrendNum) Then
MsgBox "ONLY VALID NUMBERS ALLOWED!", vbCritical, Title:="WARNING TO USER"
Exit Sub
End If
If IsNumeric(TrendNum) Then
    ActiveSheet.ChartObjects(1).Activate
    If ActiveChart.SeriesCollection.Count > 0 Then
    ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=xlMovingAvg, Period:=TrendNum _
        , Forward:=0, Backward:=0, DisplayEquation:=False, DisplayRSquared:= _
        False).Select
        With Selection.Border
        .ColorIndex = 3
        .Weight = xlMedium
        .LineStyle = xlContinuous
    End With
'................ additional code removed.........
End Sub
Unfortunately, my data is arranged in a column with the most recent data positioned at the uppermost row so the moving average trendline prematurely terminates before the end of the chart by the number of periods the moving average is (eg. if I apply a 30 period moving average, the trendline terminates 30 periods before the last data period plotted on the chart).

By default, it appears that an excel trendline (moving average type) applied to a range in a column is calculated from top to bottom starting at the uppermost row rather than from bottom to top as I would like. So the easy solution was to reverse the order of my data within the column. However, this had an adverse impact on the functionality of other features of my spreadsheet so I have to find another solution.

Question: Can you suggest a solution to force excel to calculate a trendline (moving average type) in reverse order? If you could, that would be great and it would save a lot of work.

An alternative is to insert a formula (eg. "AVERAGE(range of cells to be selected here)") into cells within a vacant column and plot the data as a new series on the chart. If I adopt this approach, I would like to retain the ability for the user to select the moving average period by entering the specified period into an InputBox popup as per my existing code.

Question: Can you suggest a vba solution that would accomplish this task?

As always, thanks for your help.

Ps. Still using Excel 97.

Kind regards

Wayne
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,216,102
Messages
6,128,853
Members
449,471
Latest member
lachbee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top