View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Bruno Campanini
 
Posts: n/a
Default Calculating averages excluding outliers...a question

"stew1901" wrote in
message ...

Should add, the condition to exclude points will be a simple and <
than condition.


Try this routine.
You must define in Set SourceRange your Sheet and your
range's starting cell (I have assumed there are no blank cells
in the range).
I've used as helper column the one to the right of your range.
If it is a problem an array (i.e. RAM memory) can be used instead.
List your condition to exclude in place of my example list
under If Not ( _
Let me know if it works.
===============================
Public Sub ConditionalAverage()
Dim SourceRange As Range, i, j As Long
Dim Total As Double, M1 As Double, Median As Double

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Set SourceRange = [Sheet10!BS6]
If Not IsEmpty(SourceRange(2, 1)) Then
Set SourceRange = SourceRange.Resize(SourceRange. _
End(xlDown).Row - SourceRange.Row + 1)
End If

For Each i In SourceRange
If Not ( _
(i = 10 And i <= 22) Or _
(i = 125 And i <= 150) Or _
(i = 250 And i <= 300) Or _
(i 1000) _
) Then
Total = Total + i
j = j + 1
SourceRange(j, 2) = i
End If
Next

M1 = Total / j
If j Mod 2 Then
Median = SourceRange(Int(j / 2) + 1, 2)
Else
Median = (SourceRange(j / 2, 2) + SourceRange(j / 2 + 1, 2)) / 2
End If

If j Then
MsgBox "M1 = " & Total / j & vbCrLf & "Me = " & Median
End If
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub
==========================

Ciao
Bruno