What to do when AVERAGEIFS criteria does not match any data in a macro

beedistinct

Board Regular
Joined
Jun 24, 2011
Messages
62
I'm using the following in a macro and it seems that none of the criteria match, so while running the macro it gives me a run-Time error '1004'. "Unable to get the AverageIfs property of the WorksheetFunction class". How to handle this? I need to treat the average as a zero when the criteria doesn't match.

Average = WorksheetFunction.AverageIfs(AverageRange, Range, ">12/31/2009", Range, "<1/1/2011", StateRange, "<>Buy", StateRange, "<>Created", UrgentRange, "Urgent", ConditionRange, "<>")
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Possibly like this

Rich (BB code):
Dim Average As Variant
Average = Application.AverageIfs(AverageRange, Range, ">12/31/2009", Range, "<1/1/2011", StateRange, "<>Buy", StateRange, "<>Created", UrgentRange, "Urgent", ConditionRange, "<>")
If IsError(Average) Then
    Average = 0
End If
 
Upvote 0
I wonder what is Range as fourth argument? If you use named ranges, you'd write this way: Range("MyNamedRange").
 
Upvote 0
Thank you, it worked!

The Range is actually defined as a variable with type Range. I changed its name to cull out our organization specific data.

Thanks for the tip there!
 
Upvote 0
Possibly like this

Rich (BB code):
Dim Average As Variant
Average = Application.AverageIfs(AverageRange, Range, ">12/31/2009", Range, "<1/1/2011", StateRange, "<>Buy", StateRange, "<>Created", UrgentRange, "Urgent", ConditionRange, "<>")
If IsError(Average) Then
    Average = 0
End If

Thanks so much, it worked the way you suggested!
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,942
Members
449,094
Latest member
teemeren

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