简体   繁体   中英

How to calculate an average with complex conditions in excel

I will explain this the best I can.

I have a data sheet extracted with SQL. My goal is to calculate the average of certain cells in a column. There is a condition that the row which the cell is in contains a specific letter such as "B".

I'm dealing with building permits and they all have an id with the form: "12-3456-12-B". The letter resembles the type of permit and that's how I want to divide my averages. In the column I wish to extract the averages from is a percentage that resembles the degree of repair on the said permit.

So what I want is to have an average degree of repairs on individual permit types.

On a logical perspective: "Use the value if Cell B in current row contains ie "-B""

Cheers!

If the IDs are in column B and the %s in column C this is quite simple using AVERAGEIF function in Excel with a "wildcard" , eg

=AVERAGEIF(B:B,"*B",C:C)

That formula averages column C where the column B entry on the same row ends with "B"

What have you tried so far? Any specific problems?

From my point of view you've basically got two choices:

1) You use Excel formulas

2) You use VBA (recommended)

Using option 1 can be done several ways. The easiest is probably using a new column for each permit type and then just using simple IF formulas in there to get the value in case the RIGHT(yourCell, 1) is B or C or whatever. Then you do a COUNTIF to get the number of entries in that column which will enable you to calculate the average of the columns values.

Another way would be via VLOOKUP . Using this you can build up your own matrix and then calculate the averages (I would prefer this one over the approach above).

Now for option 2 (VBA):

This won't bloat up your actual excel sheet, which is a big plus imho. Get an array for every permit type and just use a simple loop to parse every row (with content) in the sheet and add the degree of repair value to the appropriate array. Once finished just parse each array, calculate your averages and write them back into the sheet to some nice result table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM