Combining Filter and AverageIfs

beedistinct

Board Regular
Joined
Jun 24, 2011
Messages
62
I wish to filter my data using the macro and then apply the AverageIfs function only on the filtered data. I've no idea how to restrict the AverageIfs function just to the data that is filtered.
The below is what I have so far. However, The AverageRange is the entire Range and not the filtered rows only.

Code:
Dim AverageCount As Long
 
    Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$C$14").AutoFilter Field:=2, Criteria1:="Active"
    Set AverageRange = Range("A1:A13")
    Set CriteriaRange = Range("C1:C13")
    AverageCount = Application.AverageIfs(AverageRange, CriteriaRange, "<=1/11/2013")
    
    
    MsgBox ("Average of Active cells is : " & AverageCount)
 

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.
I wish to filter my data using the macro and then apply the AverageIfs function only on the filtered data. I've no idea how to restrict the AverageIfs function just to the data that is filtered.
The below is what I have so far. However, The AverageRange is the entire Range and not the filtered rows only.

Code:
Dim AverageCount As Long
 
    Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$C$14").AutoFilter Field:=2, Criteria1:="Active"
    Set AverageRange = Range("A1:A13")
    Set CriteriaRange = Range("C1:C13")
    AverageCount = Application.AverageIfs(AverageRange, CriteriaRange, "<=1/11/2013")
 
 
    MsgBox ("Average of Active cells is : " & AverageCount)
Here's the array formula** that would be entered on the worksheet:

=AVERAGE(IF(SUBTOTAL(2,OFFSET(A1,ROW(A1:A13)-ROW(A1),0,1)),IF(C1:C13<=DATE(2013,11,1),A1:A13)))

** array formulas need to be entered using the key
combination of CTRL,SHIFT,ENTER (not just ENTER).
Hold down both the CTRL key and the SHIFT key
then hit ENTER.

Maybe you can figure out how to write it into your code.

I assume the date was in the format of d/m/y.
 
Upvote 0
Using Biff's approach, maybe something like this...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] test()

    [color=darkblue]Dim[/color] LastRow [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] Avg [color=darkblue]As[/color] [color=darkblue]Double[/color]
    
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    [color=darkblue]With[/color] Range("A1:C" & LastRow)
        .AutoFilter field:=2, Criteria1:="Active"
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    Avg = Application.Evaluate("AVERAGE(IF(SUBTOTAL(3,OFFSET(A2:A" & LastRow & _
                ",ROW(A2:A" & LastRow & ")-ROW(A2),0,1))>0,IF(C2:C" & LastRow & _
                "<=DATE(2013,11,1),A2:A" & LastRow & ")))")
    
    MsgBox "The average is:  " & Avg
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]

To clear the filter, the following line of code can be added...

Code:
ActiveSheet.AutoFilterMode = False

Note that the formula will need to be amended if Column C and/or Column A contain empty cells.
 
Upvote 0
Thank you for the guidance!! I do not understand how the array formula works.

Could you please point me to some reference material that can help me understand/interpret array formulas?
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,729
Members
452,939
Latest member
WCrawford

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