.Net Code Monkey RSS 2.0
 Tuesday, December 02, 2008

I have recently been lumped with quite a bit of Excel based tasks to do at work, and there is one function that I keep needing that does not appear to be in VBA. "AverageIf"

Knowing that you can perform the equivelant of an AverageIf with combination of SumIf() and CountIf(), I thought I'd wrap these up in a function.

'Purpose:
'   Like Excel's sum if function put performs an average, rather than a sum.
'
Public Function AVGIF(ByVal checkRange As Range, ByVal criteria As Variant, ByVal avgRange As Range) As Variant

    Dim sumIfResult As Double
    Dim countIfResult As Double

    sumIfResult = Application.WorksheetFunction.SumIf(checkRange, criteria, avgRange)
    countIfResult = Application.WorksheetFunction.CountIf(checkRange, criteria)

    If countIfResult = 0 Then
        AVGIF = 0
    Else
        AVGIF = sumIfResult / countIfResult
    End If

End Function

I hope some one finds this as useful as I have.

 

Tuesday, December 02, 2008 5:01:14 PM (GMT Standard Time, UTC+00:00)  #    Comments [4] -
VBA | Excel
Wednesday, December 03, 2008 12:28:35 PM (GMT Standard Time, UTC+00:00)
If you have Excel 2007, the AVERAGEIF function is built in.

Otherwise you could simulate your function directly on the worksheet, like: =IF(SUMIF(Range,criteria,SumRange)/COUNTIF(Range,criteria)=0,0,SUMIF(Range,criteria,SumRange)/COUNTIF(Range,criteria))
Wednesday, December 03, 2008 12:49:47 PM (GMT Standard Time, UTC+00:00)
Also remember to use Application.volatile False in your function.

This will prevent the function being called each time excel decides
to calculate - particularly annoying while you are stepping through the code...

The Real JP
Wednesday, December 03, 2008 12:50:21 PM (GMT Standard Time, UTC+00:00)
It's already in 2007, hey? That is good to know as we will be moving to that in the not too distant future, I believe.
Dib
Monday, December 08, 2008 7:25:16 PM (GMT Standard Time, UTC+00:00)

Also remember to use Application.volatile False in your function.
</quote>
Cheers John, I must add that in to that function and others I have used.
Dib
All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
Archive
<December 2008>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Blogroll
 Clemens Vasters
 Harry Pierson
Passion * Technology * Ruthless Competence
 Joshua Flanagan
A .NET Software Developer
 Michael Schwarz's Blog
Developing applications on the Microsoft platform since Windows 3.1!
 Omar Shahine
Yet another Microsoft blogger
 Scot GU
Scott Guthrie lives in Seattle and builds a few products for Microsoft
 Scott Hanselman
Programming Life and the Zen of Computers
 Tom Mertens
Tom's corner
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Duane Wingett
Sign In
Statistics
Total Posts: 39
This Year: 4
This Month: 0
This Week: 0
Comments: 39
Themes
Pick a theme:
All Content © 2012, Duane Wingett
DasBlog theme 'Business' created by Christoph De Baene (delarou)