View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default Average If Macro

On 1 Jun 2004 12:04:16 -0700, (Kris Taylor) wrote:

Hi all!

I'm not quite sure whether this is feasible or not, however I guess I
can give it a shot.

I have a worksheet with 3 different tabs: i) Jan-Jun03 ii) Jul-Dec03
iii) Jan-Apr04

In each of these tabs I need to average a range of criteria in column
"D". The entries in column D range from -5000 to 6000 or so and it
goes on for about 50000 rows in each tab. Here's the catch; I need
to average based on intervals. I need it to give me an average for
the range of data between entries that go below 15. So basically,
everytime the data goes below 15, I should get an average based on all
prior data greater than 15. When an entry goes below 15 is totally
random.

If all the average intervals could go in column "M", that would be
great!!!

If further information is required, do not hesitate to tell me!

Feel free to post or email me!!!

Thanks in advance,

Kris Taylor


Your description excludes values that are equal to 15 from being averaged. Is
that what you want? I thought not, so they are included in the VBA solution
I've posted below; but could easily be excluded:

=================================
Option Explicit
Sub AvgGT15()
Dim AOI As Range
Dim StoreResult As Range
Dim c As Range
Dim i As Long
Dim Result()

Set StoreResult = [M1]
Set AOI = [D1:D500]
[M1:M5000].ClearContents

i = 0
ReDim Preserve Result(i)

For Each c In AOI
If c.Value < 15 Then
If Result(0) = 15 Then
StoreResult.Value = Application.WorksheetFunction.Average(Result())
Set StoreResult = StoreResult.Offset(1, 0)
End If
i = 0
Result(0) = 0
Else
ReDim Preserve Result(i)
Result(i) = c.Value
i = i + 1
End If
Next c

If Result(0) = 15 Then
StoreResult.Value = Application.WorksheetFunction.Average(Result())
Set StoreResult = StoreResult.Offset(1, 0)
End If

End Sub
========================
--ron