Share via

Is it possible to code Excel COUNTIF with multiple criteria using variables?

Anonymous
2019-08-17T14:23:23+00:00

Consider this formula used in an Excel worksheet:

=COUNTIF(D133:D164,{"<140",">100"})

It gives me exactly the information I need from a table of blood pressure readings. The only thing wrong with it is that the criteria values are hard-coded.

Is it possible to code this so using variables that will be defined elsewhere on the sheet? The use of the brackets seems to be the obstacle.

I know there are other methods to get the same info, but if it is possible to use this one I'd like to know how to do it.

Thanks.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

DaveM121 891K Reputation points Independent Advisor
2019-08-17T16:55:42+00:00

Hi Tom

Does this formula work for you?

=COUNTIFS(D133:D164,"<"&A1, D133:D164,,">"&A2)

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anonymous
    2019-08-17T16:48:27+00:00

    Dave,

    No joy yet I'm afraid. The statement can be entered without throwing an error, but it returns a 0, which should not be. I stepped through the Evaluate Formula and confirmed that A1 and A2 were resolving correctly, but then I hit a #VALUE error. Still looking at that. There may be another piece but I think we are close.

    Was this answer helpful?

    0 comments No comments
  2. DaveM121 891K Reputation points Independent Advisor
    2019-08-17T15:45:17+00:00

    Hi Tom

    Sorry, that is my fault, here is the working version . .

    =COUNTIF(D133:D164,(AND("<"&A1,">"&A2)))

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2019-08-17T15:41:19+00:00

    I tried making the change as per your suggestion and it threw an error. ("There is a problem with this formula.")

    So I next tried copying the statement directly from your response to eliminate the possibility of my making a typo. I made sure there were values in A1 and A2. Got the same error. 

    I got the same response last night with a similar concatenation, but using Defined Names. It just doesn't like those brackets!

    If you can enter that statement without getting a syntax error I'm not sure what the problem could be.

    Was this answer helpful?

    0 comments No comments
  4. DaveM121 891K Reputation points Independent Advisor
    2019-08-17T14:26:46+00:00

    Hi Tom,

    Yes, you can easily do that if you use a formula like this, say of those values were in A1 and A2

    =COUNTIF(D133:D164,{"<"&A1,">"&A2})

    Was this answer helpful?

    0 comments No comments