Need to create a loop that averages numbers within 'n' standard deviations

embekay

New Member
Joined
Aug 12, 2013
Messages
7
I am trying to design a function that grabs an average of only numbers that are within a certain deviation of the mean. I got the plan down, and have done this before without VBA, its just a user defined function will be much quicker.

The gist of what I am doing:
Find the standard deviation of a range and then only find the average of numbers that fall within n deviations from the mean. I am stuck with the part that I would normally use an array code for. Here is what my array would look like:

{=AVERAGE(IF((nums>high)*(nums<low),nums))}
<low),nums))}
[]

<low),nums))}
Code:
Function ConfidenceAvg(nums As Range) As Double
    Dim stdDev As Double
    Dim high As Double 
    Dim low As Double
    Dim average As Double
    stdDev =   WorksheetFunction.StDev_S(nums)
    average =  WorksheetFunction.average(nums)
    high =       average + stdDev 'one deviation from mean
    low =        average - stdDev 'one deviation from mean
    'enter array loop here to get only specified averages
 ConfidenceAvg = 
End Function

There are a couple ways I thought of tackling this, but not so elegant due to my newbie level of VBA
Appreciate your replies.</low),nums))}></low),nums))}
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Not sure why my array pseudo code isn't posting, must be some bug with the forum parsing. Here is though:
{=AVERAGE(IF((nums < high)*(num<low),nums)}< html=""></low),nums)}<>

edit: OK I give up, I have no idea how to post this array code to look like excel, here is another try at it

{Average(if(nums "are less than" high) and (nums "are greater than" low),nums)}
 
Last edited:
Upvote 0

Forum statistics

Threads
1,226,730
Messages
6,192,705
Members
453,748
Latest member
akhtarf3

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