Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel 2007, AVERAGEIFS Syntax problem in VBA

Status
Not open for further replies.

JA3395

Programmer
May 22, 2007
88
IE
Has anyone successfully used the WorksheetFunction AVERAGEIFS?

I'm having real trouble figuring out the syntax for the function in VBA.

Code:
    strLowerDate = Format(dblRunDate + TimeSerial(0, 0, 1), "dd/mm/yyyy hh:mm:ss")
    strUpperDate = Format(dblRunDate + TimeSerial(23, 59, 59), "dd/mm/yyyy hh:mm:ss")

This doesn't work -
Code:
   dblResolved = Application.WorksheetFunction.AverageIfs(Range("Resolved"), _
                                                            Range("Language"), Language, _
                                                           Range("Resolved"), ">=" & strLowerDate, _
                                                            Range("Resolved"), "<=" & strUpperDate)

This does work -
Code:
   dblResolved = Application.WorksheetFunction.AverageIfs(Range("Resolved"), _
                                                            Range("Language"), Language)

So it's something to do with the string syntax of the criteria for the date range.

NB: Range("Resolved") is a list of Date/Times

Please help!
 
Hi,

First, the function is AVERAGEIF NO S.

Second, the funcion has THREE arguments...

Range
Criteria
Sum Range.

I'd suggest getting th function to work first on the sheet, WITHOUT VBA code. Then, with that as a "template" code it.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Sorry to disagree. In Excel 2007 there is a function AVERAGEIFS.

Similar to AVERAGEIF but it allows multiple conditions.

Try it and see!
 
I have got it to work on the sheet using EXCEL functions.

Code:
=AVERAGEIFS(Resolved,Language,"="&$F$2,Worktype,$F3,Resolved,">="&TEXT(DATE(YEAR(F$5),MONTH(F$5),DAY(F$5))+TIME(0,0,0),"dd/mm/yyyy hh:mm:ss"),Resolved,"<="&TEXT(DATE(YEAR(F$5),MONTH(F$5),DAY(F$5))+TIME(23,59,59),"dd/mm/yyyy hh:mm:ss"))

I'm trying to code it in VBA because by the time I've done IF(ISERROR( etc and done the subtraction of a parallel date, then the algorithm gets very long and complicated, so I'm trying to do it in VBA.

BTW, there is now COUNTIFS and SUMIFS as well....
 
I stand corrected!

Arg 1: Range to average
Arg 2: Criteria 1 range
Arg 3: Criteria 1
Are 4 & 5 are sot Criteria 2.

Again, get it working on the sheet.

Is it working there?

Post the FORMULA that is working on your sheet. Chnaces are that you will have to play with the QUOTES in orser to get the date/time string within quotes IN the formula.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
OK, I've solved it, it didn't like the string date.

Code:
dblRunDate = DateSerial(Year(RunDate), Month(RunDate), Day(RunDate))
dblLowerDate = dblRunDate + TimeSerial(0, 0, 1)
dblUpperDate = dblRunDate + TimeSerial(23, 59, 59)
    
dblResolved = Application.AverageIfs(Range("Resolved"), _
Range("Language"), Language, _
Range("Worktype"), Worktype, _
Range("Resolved"), ">=" & dblLowerDate, _
Range("Resolved"), "<=" & dblUpperDate)

Why, I don't know, but at least I'm getting a result not an error!

Thanks for any bain-power out there that has been put into this...
 
Just as a hint, to anyone trying out these WorksheetFunction(s)

If you get the error:

Unable to get the AverageIfs property of the WorksheetFunction Class

Code:
Application.WorksheetFunction.AverageIfs()

Then use the code

Code:
Application.AverageIfs()

Then you will get the true error being returned when the function is executed...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top