Count the number of cells a formula references

domtrump

Board Regular
Joined
Apr 1, 2010
Messages
245
I have a formula that sums up specific cells above it in the column. (i.e. a50=a1+a7+a8+a13...). Sometimes we take one of these cells out of the mix due to specific circumstances. I need to create a cell that indicates the number of cells being referenced by this formula for each column so that we know that a special condition is being applied. Is there some way to COUNT the number of cells being referenced by another formula?

Thanks.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi,

Take a look here.....
Show FORMULA or FORMAT of another cell

And then try something like this....

Excel Workbook
ABCD
1SumFormula UsedResult
2893.00=B5+B10+B153
3
4NameAmount
5Bart Simpson368.00
6Lisa Simpson277.00
7Bart Simpson474.00
8Lisa Simpson163.00
9Maggie Simpson346.00
10Homer Simpson100.00
11Homer Simpson147.00
12Bart Simpson267.00
13Maggie Simpson299.00
14Homer Simpson399.00
15Bart Simpson425.00
16Bart Simpson496.00
17
Results


You will need to use....
=GetFormula(A2)
In one cell to display the sum formula and then in a different cell use.....
=LEN(B2)-LEN(SUBSTITUTE(B2,LEFT(SUBSTITUTE(B2,"=",""),1),""))
to count the number of cells used in your original sum formula.

Or you could join it all together in one cell.....
=LEN(GetFormula(A2))-LEN(SUBSTITUTE(GetFormula(A2),LEFT(SUBSTITUTE(GetFormula(A2),"=",""),1),""))

I hope that helps.

Good luck.

Ak
 
Upvote 0
I have a formula that sums up specific cells above it in the column. (i.e. a50=a1+a7+a8+a13...). Sometimes we take one of these cells out of the mix due to specific circumstances. I need to create a cell that indicates the number of cells being referenced by this formula for each column so that we know that a special condition is being applied. Is there some way to COUNT the number of cells being referenced by another formula?
As long as your formula looks like you showed us (highlighted in red above) with individual plus signs between individual cell references, you can use this UDF to count how many cell references there are...
Code:
Function CellsBeingSummed(R As Range) As Long
  Application.Volatile
  CellsBeingSummed = Len(R.Formula) - Len(Replace(R.Formula, "+", "")) + 1
End Function
HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use CellsBeingSummed just like it was a built-in Excel function. For example,

=CellsBeingSummed(A50)
 
Upvote 0
Hi,

Erik, I do like the non VBA solution. :)

Just out of curiosity folks
What would be the best solution if the SUM were a range, eg..
=SUM(A2:A20)

Any ideas?

Ak
 
Upvote 0
assuming you have only =SUM(yourrange), you can use
=ROWS(INDIRECT(MID(CellFormula,SEARCH("(",CellFormula)+1,LEN(CellFormula)-SEARCH("(",CellFormula)-1)))*COLUMNS(INDIRECT(MID(CellFormula,SEARCH("(",CellFormula)+1,LEN(CellFormula)-SEARCH("(",CellFormula)-1)))
This is multiplicating rows and columns.

I would prefer though to use a helper column for the cellformula and the search instead of this long formula, especially if you need a lot of them.

If all cells are populated with numbers, you can use
=COUNT(INDIRECT(MID(CellFormula,SEARCH("(",CellFormula)+1,LEN(CellFormula)-SEARCH("(",CellFormula)-1)))
 
Upvote 0
Thanks for that Erik.

I'm going have to save a link to this thread for future reference. ;)

Ak
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,497
Members
448,967
Latest member
visheshkotha

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
Back
Top