amending macro

karolina1406

Board Regular
Joined
Apr 18, 2016
Messages
110
Office Version
  1. 365
Platform
  1. Windows
hi,

i used fab macro script to plot average on the chart. However, i noticed that the macro calculates an average taking into account elements with 0 values. Does anyone know how to amend below to exclude from calculations elements with zero value?

​Sub AverageLine()'Update 20130907
Dim ser As Series
Dim arr As Variant
Dim total As Double
Dim outArr As Variant
If <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: dotted; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(0, 0, 0); border-left-color: initial; border-image: initial; cursor: help;">VBA</acronym>.TypeName(Application.Selection) <> "Series" Then Exit Sub
Set ser = Application.Selection
arr = ser.Values
total = Application.WorksheetFunction.Average(arr)
ReDim outArr(LBound(arr) To UBound(arr))
For i = LBound(outArr) To UBound(outArr)
outArr(i) = total
Next
With ActiveChart.SeriesCollection.NewSeries
.XValues = ser.XValues
.Values = outArr
.Name = "Average " & ser.Name
.AxisGroup = ser.AxisGroup
.MarkerStyle = xlNone
.Border.Color = ser.Border.Color
.ChartType = xlLine
.Format.Line.ForeColor.RGB = RGB(255, 0, 0)
.Format.Line.DashStyle = msoLineDash
End With
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I would have thought you would change this

total = Application.WorksheetFunction.Average(arr)

to

total = Application.WorksheetFunction.Averageifs(arr,arr,">0")

 
Upvote 0
I would have thought you would change this

total = Application.WorksheetFunction.Average(arr)

to

total = Application.WorksheetFunction.Averageifs(arr,arr,">0")


thank you for this... however, I get some error which says" Run-time error '424': object required.... :eek:
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,356
Members
449,080
Latest member
Armadillos

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