Good day,
I use the formula =COUNTIF(B4:B13;C4) in cell D4. The range B4:B13 covers 10 cells. I want to link the range to cell D1. I want to change the range based on what I type in D1. Is this possible. Thank you.
Kenny Robertson
Good day,
I use the formula =COUNTIF(B4:B13;C4) in cell D4. The range B4:B13 covers 10 cells. I want to link the range to cell D1. I want to change the range based on what I type in D1. Is this possible. Thank you.
Kenny Robertson
Hi Kenny and welcome to the Forum.
You can use the worksheet function INDIRECT for this. If D1 contains valid range text e.g. B4:B13, then the range in the COUNTIF formula is replaced by the bit in bold below:
=COUNTIF(INDIRECT(D1), C4)
As you alter the text in D1, the tested range will change (but you'll get a #REF error if it isn't a valid range).
REVISION #1, 02 June 2025
Given Willie has given an enhanced Answer (fixing the range if D1 is ""), here's another way to avoid the error- wrap an IFERROR function around the basic formula (changes in bold below):
= IFERROR(COUNTIF(INDIRECT(D1),C4),"Check D1")
Now D4 will contain a count but state Check D1 if D1 is blank or doesn't contain a valid range (e.g. says B4-B14). You can change the error message (within the quotation marks above).
That formula is demonstrated in the attached file.
Hope this fixes things for you. If so, please remember to mark this Answer as Selected. Otherwise please check and Select Willie's Answer.
Hi Kenny,
My answer is similar to John's but with a twist.
=IF(D1 = "", COUNTIF(B4:B13,C4), COUNTIF(INDIRECT(D1),C4))
This formula will check if D1 contains something. If not, then it will use the original COUNTIF formula. If D1 does contain something, then COUNTIF will use that range. And as John has mentioned, you will get an error if D1 is not a valid range.
If John's or my answer solves things, please mark it as Selected.
Cheers :-)