ExcelFilter: How do I display the sum within that column without resulting in a "circular reference" error?
-
ExcelFilter: How do I display the sum within that column without resulting in a "circular reference" error? I'm building a spreadsheet to keep track of my paychecks. I'd like to keep track of YTD totals of a column (eg, federal taxes) at the top of that column, and find a formula that I can enter once into the top that will calculate the sum for the rest of the column for as many entries as I choose to make. Simply summing the entire column results in a circular reference error. In other wods, for the simplified example below, give me a function for [SUM_FEDERAL] (in B1) that will sum the individual values for each cheque without me having to adjust/copy the function for each entry of a new cheque. (I know I could simply do =SUM(B4:B100), and adjust after 100 entries, but I want a solution that both scales and makes it simple to maintain). AB 1YTD [SUM_FEDERAL] 2 3 Cheque No.    Federal 4015 5028 6037 I imagine that there is a super simple solution -- after several hours of playing around and searching Google, I've given up and turned to MeFi.
-
Answer:
I think you're being a little too much of a purist here. It is possible however -- you need to use a UDF (User Defined Function). Here's a couple of ways of doing it. (1) Only works for single column, non contiguous range -- but it's fast. Place the following formula in your cell B1: =SUMRANGEWITHEXCEPTION1(B:B,B1:B4) And insert a macro module into your workbook and place the following code in there: Public Function SUMRANGEWITHEXCEPTION1(rng1 As Range, rng2 As Range) As Double Dim rngToSum As Range Dim lCountExclusions As Long Dim lRow As Long ' Must be single column and non-contiguous range ' get a new range that excludes rng2 from rng 1 ' we'll use lcount to tell when we've finished the exclusion ' and can therefore stop -- to speed it up With rng1 For lRow = 1 To .Rows.Count If lCountExclusions = rng2.Cells.Count Then Exit For If Intersect(.Cells(lRow, 1), rng2) Is Nothing Then If rngToSum Is Nothing Then Set rngToSum = Range(.Cells(lRow, 1)) Else Set rngToSum = Union(rngToSum, .Cells(lRow, 1)) End If Else lCountExclusions = lCountExclusions + 1 End If Next lRow 'On Error Resume Next If rngToSum Is Nothing Then Set rngToSum = Range(.Cells(lRow, 1), .Cells(.Rows.Count, 1)) Else Set rngToSum = Union(rngToSum, Range(.Cells(lRow, 1), .Cells(.Rows.Count, 1))) End If On Error GoTo 0 End With SUMRANGEWITHEXCEPTION1 = WorksheetFunction.Sum(rngToSum) Set rngToSum = Nothing End Function (b) A more universal solution (but slower): use : =SUMRANGEWITHEXCEPTION2(B:B,B1:B4) and the following code: Public Function SUMRANGEWITHEXCEPTION2(rng1 As Range, rng2 As Range) As Double Dim c As Range Dim rngToSum As Range Dim rngNonBlanks ' This supports multiple columns and non-contiguous ranges ' get a new range that excludes rng2 from rng 1 For Each c In rng1.Cells If Intersect(c, rng2) Is Nothing Then If rngToSum Is Nothing Then Set rngToSum = c Else Set rngToSum = Union(rngToSum, c) End If End If Next c SUMRANGEWITHEXCEPTION2 = WorksheetFunction.Sum(rngToSum) Set c = Nothing Set rngToSum = Nothing End Function All they do is sum column B as the normal sum function does. But they exclude from the sum the range passed as the second argument thereby sidestepping the circular range issue. I think I have a neater solution -- but I have to go dig up weeds. :(
chefscotticus at Ask.Metafilter.Com Visit the source
Other answers
If I am picturing this correctly... and maybe I am not... Why not make a sum cell way at the bottom of the column (A500 or whatever) then make A1 (=A500) I must be missing something.
The Deej
(Hmm. Tables don't work in MeFi apparently. See the formatted example here: http://img39.picoodle.com/img/img39/8/6/30/f_askmefiexcem_0896809.jpg)
chefscotticus
You're concerned about outgrowing whatever limit you set, right? In Excel 2000, http://support.microsoft.com/kb/264626 So you'll never outpace SUM (B2:B65536).
ormondsacker
I don't use Excel, but I think a better formulation of this question is that you want a function that does: Sum the cells in a column starting from a fixed address n to the last non-blank cell in that column. Although you are probably over-thinking the whole thing and I recommend ormondsacker's fix.
demiurge
For column B:=sum(b:b-b1)
b1tr0t
(put that formula in cell b1, obviously)
b1tr0t
@ b1tr0t: sorry mate, doesn't seem work.
chefscotticus
Did you mean to best-answer yourself? I don't have Excel loaded on this computer to test, but if you're looking for more elegance, I think this should work as a generalized solution: =SUMIF(A:A, $A$1, B:B) Basically, that looks at each row in turn to see if the A value is the same as the cell in A1 - if it's different, it adds that B value to the overall total. The way your spreadsheet is set up, that should add all B values except the one in row 1, preventing a circular reference. I think Excel is smart enough to pull that off, but check it first.>
ormondsacker
If you insert a new line among the lines being summed, then the sum will automatically be adjusted to include the new entry as well as all the previous entries. This works best when the sum is at the end, forcing you to insert new lines to add more data.
alexei
Related Q & A:
- How do I display all categories on a page with a corresponding image?Best solution by Magento
- How do I use auto-increment within a trigger?Best solution by dba.stackexchange.com
- How can I evaluate an equation within a String?Best solution by stackoverflow.com
- How do I display my new Yahoo name within a group?Best solution by answers.yahoo.com
- How can I sell my car within a week?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.