how to use averageifs in VBA

Rookieeee

New Member
Joined
Aug 26, 2019
Messages
22
Hello guys,

Currently I have a set of commands in VBA like:
Rich (BB code):
for n = 1 To 274
port_return.Columns(3).Cells(n).Value = WorksheetFunction.AverageIfs(monthlydata.Columns(2), monthlydata.Columns(3), "=W", monthlydata.Columns(5), "<=0.3")

port_return.Columns(4).Cells(n).Value = WorksheetFunction.AverageIfs(monthlydata.Columns(2), monthlydata.Columns(3), "=W", monthlydata.Columns(5), ">0.7")


Next n

However, in this averages function, sometimes there is no cell meeting the criteria in averages(), so this 'For next' loop stops when there is no cell meet those criteria.

What I need to do is that this function returns '0' or blank when no cell meets criteria.

I tried this command
Rich (BB code):
Rich (BB code):
Rich (BB code):
port_return.Columns(4).Cells(n).Value = WorksheetFunction.IfError(WorksheetFunction.AverageIfs(monthlydata.Columns(2), monthlydata.Columns(3), "=W", monthlydata.Columns(5), ">0.7"), "")
but it does not work.

Can you guys please help me out?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try it like
Code:
x = Application.AverageIfs(monthlydata.Columns(2), monthlydata.Columns(3), "=W", monthlydata.Columns(5), "<=0.3")
If Not IsError(x) Then port_return.Columns(3).Cells(n).Value = x
 
Upvote 0
Try it like
Code:
x = Application.AverageIfs(monthlydata.Columns(2), monthlydata.Columns(3), "=W", monthlydata.Columns(5), "<=0.3")
If Not IsError(x) Then port_return.Columns(3).Cells(n).Value = x
Thank you so much. It works. Additionally, I also tried 'on error resume next', it's also a good choice.

Anyway, thank you so much
 
Upvote 0
You're welcome & thanks for the feedback.
It's also best to avoid 'on error resume next' unless absolutely necessary as it can mask errors & cause other problems.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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