views:

207

answers:

3

I would like to have part of an excel formula be dynamic, other than a cell reference.

For instance, suppose that in column A (cells A1:A99) I have a bunch of numbers, and I want to know how many of those numbers are greater than 50.

If I wanted this calculation to be static, I could simply use one of the following:

=COUNTIF($A$1:$A$99,">50")
=SUM(IF($A$1:$A$99>50,1,0))
=SUM(($A$1:$A$99>50)*1)

I mention all three because my actual formula is hairy and a bit of a mix of the second and the third. (After all, perhaps something will work with COUNTIF but not with the others.)

Now, I want to be able to type my condition in another cell (say C1). So if I type ">50" in C1, my calculation will be as above, but if I type "<100" I will count how many entries of column A are less than 100.

Is this possible? (I am using Excel 2003 on Windows XP.)

+1  A: 

There may be something that I'm missing. If you give

=COUNTIF($A$1:$A$99,C1)

in any cell, and then in cell C1 you type >50 or <100 don't you get what you want?

gd047
Hm, you are right.I was hoping to find something I could use with my existing format (starting with `SUM`), rather than switch to `COUNTIF` or `SUMIF`. But I think I can make it work with `SUMIF`. So thank you for pointing out the obvious, which I missed.
Seth
I remember why I didn't want to use `COUNTIF` or `SUMIF`: In actuality I need multiple condition, based on more than just the cells I want to count.For instance, say the numbers in question are in B1:B99, and in the A column are dates. I will want to how many of the numbers in column B are >50 for which the date in column A is in a specific range, or on a Monday.It appears that Excel 2007 has a built-in function `SUMIFS` to deal with this, but I have 2003.
Seth
then you could use something like SUMPRODUCT((A1:A99="a")*(B1:B99="b"))
gd047
Yes, but can I make that equal sign be dynamic? I'm happy to have an `(A1:A99="Monday")` in there (and yes, `"Monday"` can be dynamic), but how to make `="b"` be dynamic, so I could set it to `>50` or `=500` by changing an outside cell.
Seth
+1  A: 

Use INDIRECT

=INDIRECT(COUNTIF($A$1:$A$99,">50"))

is same as

=COUNTIF($A$1:$A$99,">50")

But, as you identified, the former, you can generate within the excel cells! I do it all the time, for such things.

Lakshman Prasad
becomingGuru, this looks promising, but Excel gives me a `#REF!` error when I try it. When I trace the calculation steps, the final one before an error is `INDIRECT(6)` and then it (correctly) tells me that 6 isn't a valid reference. How to fix?
Seth
Well, 6 obviously isn't a valid reference, so concatenate('A',6) to get A6 :)
Lakshman Prasad
I mean that 6 is the output of the COUNTIF function (specifically, how many entries in column A are greater than 50), which you suggested should become the input to indirect. Can you clarify what should be going on here?
Seth
A: 

I usually solve this by adding another column carrying the result of a complex logical expression, like

=AND(OR(C3<D3;E3>=100);A3=VLOOKUP(B3;Sheet2!$A$2:$B$212;2;FALSE))

this formula is in all rows of -say- column F - note: no IF needed here!

then I calculate a complex conditional sum across column E using =SUMIF() like

=SUMIF(F2:F57;TRUE;E2:E57)

I know that some users say "I do not want to change my design. That's ok; my argument is that I have better control over the condition, I can work on the condition seperately from summing up or other functions that rely on that condition, and I can filter records for TRUE or FALSE to look at the subsets and have a rapid overview if the formula makes sense

hope that helps Good luck MikeD

MikeD