Sum/Count Formulas auto adjust for inserted rows - excel

Looking to create a sum and a count formula that will automatically adjust itself for new rows that are inserted within the range.
For example if I have the formula in cell D55 =SUM(D17:D54). Every time I insert a new row within that range, I need to change the top range of my formula to account for it.
Is there a way to write a formula that will automatically adjust itself, so that every time I add a new row I will not need to change my summation formula?

Try
D55: =SUM(INDIRECT("D17:D"&ROW()-1))
This should dynamically adjust to added rows since when adding rows at row 17 the current value at D17 shifts to D18 and no value is present at D17. INDIRECT() should take this into account. ROW()-1 ensures that even when rows are added immediately preceding the formula these are still taken into account.
Edit: I should have added that this can be applied to any formula. Simply replace the range part of your formula with the INDIRECT.
And a quick explanation: the INDIRECT creates an Excel reference from a string so you can construct your formula using dynamic objects. The ROW part of the formula acts as the dynamic factor which is completely dependent on the row count that you add but is independent of the position at which you add your new row.

I think people are misunderstanding. You're inserting a row at the TOP of your range correct?
If so, I would suggest formatting the range as a table. That way your table could look like this:
And the sum function would be simply:
=SUM([Data])
when a row is inserted above the 7, everything is automatically updated.

You might want to have a look at this excellent link that talks about using tables to hold your ranges. This would be my recommendation ... wrap your data in a table. This will create a structured yet flexible reference.
As is noted here, and also from experience, "Because table data ranges often change, the cell references for structured references adjust automatically. For example, if you use a table name in a formula to count all the data cells in a table, and you then add a row of data, the cell reference automatically adjusts."

Related

Auto update adjacent cells?

I have a table in excel which is constantly having new rows added. I then have another sheet (lets call it results) which is referencing this table, and counting the values in each column.
Every time I add a new record/row to my table, the results sheet throws a bunch of errors in every cell that references the column. Because I'm using a fixed range in the formula to count the column, e.g. =SUM(A1:A10), I then need to manually click each cell in results and select 'include adjacent cells' to get the count to reflect the new data. I have loads of cells in this table and I'm adding data daily so this is a very long winded way of doing things.
I'm looking for a way that I can automatically update these cells, either by using a macro, or adjusting the formula so that it will automatically increase the range of the SUM when I add new data.
The best solution I've had so far was to simply define the range to include the row below my last piece of data (i.e. if A10 is my last record, the formula would be =SUM(A1:A11)). This works, and auto increments the formula when a new cell is added. However, if I delete the record from the table for any reason, the formula reverts to only including the range containing data.
In other words, this solution works, but only until I have to delete a record. Is there any better solution that can detect which rows are populated, and auto include them in the formula?
=SUMPRODUCT(INDEX('Sheet1!'A:A,1):INDEX('Sheet1!'A:A,LOOKUP(2,1/('Sheet1!'A:A<>""),ROW('Sheet1!'A:A))))
Using INDEX and ROW reference, deleting cells won't result in broken cell references. LOOKUP is used to calculate the last non-empty cell in column A. This will auto update.
Using SUMPRODUCT instead of SUM makes it an array formula and does not require older Excel versions to enter the formula with ctrl+shift+enter (because of the LOOKUP function)
In Office 365 you could use: =SUM(FILTER('Sheet1!'A:A,'Sheet1!'A:A<>""))

How to make an INDIRECT formula but with adjusting ranges

I'm hoping someone can help me make better use of the INDIRECT formula.
I have a list of sheet names in a table and an INDIRECT formula that uses that list to return a value in a specified cell - the list of sheet names is just an easier way for me to drag the formula down the table and read the appropriate cells without having to manually link each sheet.
=INDIRECT("'"&A2&"'!"&"K10")
This works fine for single cells as the range ref is simply stated as the text in the formula (K10), the problem arises when I need to start referring to a range such as K10:K15 and summing the values.
The range K10:K15 will inevitably have new rows added or deleted on the relative tab and as INDIRECT is using text as the reference it means the range doesn't automatically adjust - this is as I understand it one of the 'benefits' of INDIRECT but in this case is actually holding me back.
Also worth noting that the range (K10:K15) may move as rows are added/deleted above this, as this range is part of a larger table.
In simplistic terms I want to achieve the same result as a standard reference to a range on another sheet, e.g. =sum(sheet1!K10:K15) (as this will adjust when rows are added/deleted) but I just want to be able to dictate which sheet is referred to via a list I have in a table on a summary sheet.
How do I either write INDIRECT so the range adjusts when new rows are added/deleted or is there a different formula I should be using that achieves this?
Any advice greatly appreciated :)
=INDIRECT("'"&A2&"'!K"& MATCH(TRUE,INDIRECT("'"&A2&"'!K:K")<>"",0)&":K"&MAX((INDIRECT("'"&A2&"'!K:K")<>"")*(ROW(INDIRECT("'"&A2&"'!K:K")))))
This indirectly references the rows from the first non empty cell up to the last non empty cell in given sheet in column K. Not sure if you need to enter with ctrl + shift + enter (not in the app version).
Note: If the range contains empty cells in between the first and last non empty cell it will be included as value 0
Or in office 365 use the following:
=FILTER(INDIRECT("'"&A2&"'!K:K"),INDIRECT("'"&A2&"'!K:K")<>"")

Selecting a Specific Column of a Named Range for the SUMIF Function

I am trying to create a SUMIF function that dynamically adds up values in a specific column of a named range in my Excel sheet.
It is very easy to do this when there is no named range :
The formula picks out all the cells that contain "London" in their name and sums up the expenses related to London.
What I am trying to do is to use a named range called TripsData (A2:B5) and tell the SUMIF function to sum the entries in the column 2 of this range that meet the criterion of having London in their name.
How can I make this work without needing to create a second named range for column 2 and simply by telling Excel to look within the specified column of this named range? Index/Match only return one value so that doesn't work when there are several cells with London in their name.
Thanks for your help!
Use INDEX to refer to a specific column in the named range (it can refer to a whole column), like this
=SUMIF(TripsData,"*London*",INDEX(TripsData,,2))
You can do that without any named ranges at all, if you turn your data into an Excel Table object. Select any cell in the range or the whole range and click Insert > Table or hit Ctrl-T.
There will be a dialog that asks if your table has headers. Yours does. Now you can reference the table and its columns by their inherent names and build your formula like this:
=SUMIF(Table1[Expense],"*London*",Table1[Cost])
You can rename the table, of course, even after the formula is in place. When you click a cell in the table, there will be a new ribbon for commands that relate to tables only. It's a very powerful tool.
Any formulas, formatting etc. that apply to a whole table column will automatically carry over into new table rows. The table column reference will adjust automatically, too, of course, so you don't have to mess with dynamic range names or re-define what a named range applies to.
Note: the formula uses structured referencing instead of cell addresses. This option can be turned off by clicking File > Options > Formulas > tick or untick "Use table names in formulas"
You can use Chris' idea of Index(Table1,,Col#) with the named range "Table1" (without creating an Excel table Object if you don't want to for some reason) and STILL avoid the problem Applez mentions in the comment below Chris' idea. Applez warns that using a constant for a column number reference is dangerous if you later insert another column before that column in the named range. You will find that Excel does NOT auto increment the constant, so your formula breaks.
Applez is right..... so DON'T use a constant, use a column number "reference" instead of a constant. For example....
=SUMIF(TripsData,"*London*",INDEX(TripsData,,Column(B1)))
If you later insert a column between A and B, Excel WILL auto increment the reference Column(B1) to Column(C1). Just don't delete B1 or Row 1 or you will get a REF error. I usually use the the header/tile "cell" (in whatever row that is in) for that table column within the Column reference (as it is highly unlikely I will ever delete the header/title cell of column of a table unless I delete the entire column). In this particular example as it turn out, B1 "IS" the the title/header cell for that column in the data table. So that is what I used for the example.
Awesome formula, just in case anyone needs to use a similar approach to FILTER a range. I used this approach
pmGendHC is the range I wanted to filter (I expect a spilled range with my data) I needed a colum (column number 13) to be different than 0
=FILTER(pmGendHC,INDEX(pmGendHC,,13)<>0)

Excel Worksheet formulas that figure out the row it is on to use in cell reference calculations

I have a worksheet with a large number of tables. Every other table is formatted the same. Each row has set columns that will not change with regard to the formula. Say... =(A * B / G) - D
I'm trying to figure out a way to write a Worksheet formula that I can cut and paste without having to alter the row reference in the formula for it to work. That way I can copy and paste the formula into each table since I cannot click and drag it down the whole worksheet.
Ideally, I'd like to keep a list of formulas in a text file that I could just cut and past and not have to alter. I would be really cool if I could do something similar for using UDFs that can figure out the Range(Cells()) location on its own.
I'm using . Thanks!
=ROW()
but from your description I am not sure why you can not copy something like $A1 down and let the 1 update as it goes down each row. Take a look at what the effect of the $ symbol before the column or cell reference does. It prevents the column letter and/or row number from changing as cells are copied from one location to another.
ROW() will return the row number of what row it is in, or the row number of a reference if you supply the reference address:
ROW(AS134) will return 134.
To use this, you would most likely need to combine it with something like INDIRECT:
=INDIRECT("A"&ROW())
That formula would reference column A and the same row as wherever the formula was placed. HOWEVER, INDIRECT is a bad choice if you are using it frequently as its a volatile function and will recalculate anytime something on the worksheet changes. As per Scott Craner's suggestion, INDEX is a non volatile function so it will not force the recalculations and would look something like:
=INDEX(A:A,ROW())

How do I sort a table that is referenced by another table?

I have this table that is made from reference to other table.
and now I want to sort from small to largest the third column, but since it is referenced from another table is not sorting. And I also dont want excel to sort the reference table and I need the table to be referenced because I will change some values on the reference table and I need them to be connected, so I just cant copy and paste the values and then sort.
Could you give me some help?
Ok, I went ahead and built a small example:
The formulas are as follows:
The 3 in Row 20 are to be copied downwards with PasteSpecial>Formulas:
in E20: =IFERROR(RANK($D20,$D$20:$D$33,1),"")
in F20: =IF(ISNUMBER($D20),"",(COUNTIF($D$20:$D$33,"<"&$D20)+1)+COUNTIF($D$20:$D20,$D20)-1)
in G20: =IF($E20="",(MAX($E$20:$E$33)+$F20),$E20)
The formula in B3 is to be copied into the whole target table with PasteSpecial>Formulas:
in B2: =INDEX(B$20:B$33,MATCH(ROW()-1,$G$20:$G$33,0),1)
Please let me know if you have any questions!
This method only works if the entries in the column you are sorting on are unique. You will need to adjust the address ranges to suit your data.
The formulas in the each column start from X and finishing in Z as you move left are as follows:
=INDEX($X$41:$X$47,MATCH(ROWS($Z$29:Z29),COUNTIF($Z$41:$Z$47,"<="&$Z$41:$Z$47),0))
=INDEX($Y$41:$Y$47,MATCH(ROWS($Z$29:Z29),COUNTIF($Z$41:$Z$47,"<="&$Z$41:$Z$47),0))
=INDEX($Z$41:$Z$47,MATCH(ROWS($Z$29:Z29),COUNTIF($Z$41:$Z$47,"<="&$Z$41:$Z$47),0))
Note only the range right after INDEX is changing. Thes are CSE or array formulas. It means you need to press CONTROL+SHIFT+ENTER when you are done editing your cell. Then copy the individual cell and paste it in the rows below the copied cell. pasting including the source cell or dragging down MAY not work. Repeat this for all three columns.
Proof of concept

Resources