SumIf (possible VBA) question

Rammy

New Member
Joined
Feb 17, 2011
Messages
11
Hi All,

I need a function (or VBA code) that will perform the following average:

1
2
5
0
1
0
0

I need to check if the last cell in the column is 0. If not, check the second to last cell. If that is not 0, check the next. If the next value is nonzero, take the average of all values including the nonzero value, and include 0 values that occur after that (all but the first value and the length of each column will be arbitrary).
For example, in the above example I would get (1+0+5+2)/4=8/4=2.

I imagine that one way of going about this would be reversing the order of the cells, and then taking average only after the first nonzero value. After the first nonzero value, however, 0s could be averaged. Maybe there is a more speed efficient way...

Any help would be much appreciated,

Thanks!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
The part I am a little confused on is the eliminating the first cell, wouldn't you know not to include that in your range, but maybe I am missing something. This will average to the last row that has a non-zero value and I just started at the row below the first cell. Let me know what I am missing or some other sample data sets and expected results?
Excel Workbook
ABC
812
92
105
110
121
130
140
15
16
Sheet1
#VALUE!

And there may be better ways to do this, once I know that, but this is a start. Also it cannot be used on version prior to 2007, so let me know what version of Excel you are using as well.

Hope that helps.
 
Upvote 0
Then you can't use whole ranges in an array formula (control+shift+enter)

This should work:

=AVERAGE(INDIRECT("A9:A"&MAX(IF($A$1:$A$65000 < > 0,ROW($A$1:$A$65000)))))

Its basically finding the max row that is not = 0 and using that as the last row in the average range.

Reduce the 65,000 number if your data will not get anywhere near that number, as it will make the formula a little quicker.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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