COUNTA with a condition in another column?

Big Monkey

Active Member
Joined
Nov 5, 2005
Messages
255
OK I've run into a booger here.

I have a simple formula now like

=COUNTA(A2:A65536)

What I want to do is get the same result, but only of column I on the same row contains "mytext"

So:

Col A
1
1
1
1

This would equal to 4 with my formula.

Col A Col B
1 mytext
1
1 mytext
1

How would I write the formula for the above to give me a value of 2?

I tried using a sumproduct, but that is totaling the numbers in column A, and I only want the counta values.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
Why use a formula? A Pivot Table will work just fine.

lenze
 

Big Monkey

Active Member
Joined
Nov 5, 2005
Messages
255
Well for one I've never used pivot tables and I have a project due soon. I'm not sure if I have time to learn a new function and change the entire layout of this spreadsheet in a few days.
 

Big Monkey

Active Member
Joined
Nov 5, 2005
Messages
255

ADVERTISEMENT

Awesome!

Thanks harry.

Question though...

=SUMPRODUCT(--(C2:C65536),--(I2:I65536=O18))

=SUMPRODUCT(--(C2:C65536<>""),--(I2:I65536=O18))

Why does the first formula sum up the values in the range, and the second formula only counts the non blank cells in that same range?

If I didn't see it work, I would think that the second formula is asking Excel to sum the values in the range if the cell is not blank. I'm glad it works, but I don't understand the logic.
 

barry houdini

MrExcel MVP
Joined
Mar 23, 2005
Messages
20,825
You need to understand how SUMPRODUCT works. In this formula

=SUMPRODUCT(--(C2:C6<>""),--(I2:I6=O18))

the (C2:C6<>"") part returns an array of TRUE/FALSE values, e.g.

{TRUE;TRUE;TRUE;FALSE}

the -- then "co-erces" this to 1/0 values, e.g.

{1;1;1;0}

SUMPRODUCT then multiplies the arrays and sums them, effectively giving you a count of rows where column I contains O18 and column C is not blank.

If you only have numbers in column C then in this formula

=SUMPRODUCT(--(C2:C65536),--(I2:I65536=O18))

the first -- is unnecessary because you already have numbers in column C so it could be

=SUMPRODUCT(C2:C65536,--(I2:I65536=O18))

To sum column C when column I is O18, although that can be more easily achieved with

=SUMIF(I:I,O18,C:C)
 

Big Monkey

Active Member
Joined
Nov 5, 2005
Messages
255

ADVERTISEMENT

OK. This formula below gives me an average I'm looking for and it works perfect

=SUMPRODUCT(C2:C65536,--(I2:I65536=O18))/SUMPRODUCT(--(C2:C65536<>""),--(I2:I65536=O18))

I am using this same formula for 5 different columns(switch column C with D, E, F, and G)

Now I want to get the average for all columns combined, C:G. I tried to change it to

=SUMPRODUCT(C2:G65536,--(I2:I65536=O18))/SUMPRODUCT(--(C2:G65536<>""),--(I2:I65536=O18))

but it gives me an error?

I tried to use the trace option to see if I can find out where the mistake was and it froze my PC :LOL:

Help? =]
 

barry houdini

MrExcel MVP
Joined
Mar 23, 2005
Messages
20,825
Try

=AVERAGE(IF(I2:I65536=O18,IF(C2:G65536,C2:G65536)))

confirmed with CTRL+SHIFT+ENTER

edit: amended formula

note: you can do it the way you're attempting, although I don't recommend it, but you need to change the syntax slightly

=SUMPRODUCT((C2:G65536)*(I2:I65536=O18))/SUMPRODUCT((C2:G65536<>"")*(I2:I65536=O18))
 

Big Monkey

Active Member
Joined
Nov 5, 2005
Messages
255
LOL

Yet another different solution to get the job done.

**** houdini, you really are magic :p
 

Forum statistics

Threads
1,137,366
Messages
5,681,074
Members
419,950
Latest member
BeckiJae

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
Top