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

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
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,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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