Use formula to dynamically modify named range - excel

I am using a bunch of named ranges in a workbook. Here is the history of how it has changed to make it more dynamic for when they ask me to modify it.
What I want to do is search the A column from 1 to 100 for the label in the A column (that I code into the named range formula) and where it finds it, the named range will know is the row. I know I will still need to update the named range if we ever change the name of a row but I would only need to update one named range as opposed to 75-100 named ranges as I do now whenever we make a change.
Here is the current Named Range that I am using:
=INDIRECT("Input!$C$23:"LastColumn&"$23")
LastColumn is a named range that contains the letter of the last column.
Here is some history on why I do it this way:
When I first created it I referenced the cells directly in formulas (no named range) and had to change all the formulas whenever I added another row, column, etc. Then I switched it to Named References. This fixed it so whenever we added a row, I did not need to change the references at all. However, I still had to change all the references whenever we changed the amount of columns. What I did was make the columns variable, so if we change the amount of columns I don't have to make any changes to the named ranges. Unfortunately, this had the unintended consequences of making it so whenever I add, remove, or move a row I need to update all the references. Now, I have an idea that will make both the rows and the columns variable. So, I can make any change without having to update any references.
I needed to do it this way because most of the time the columns will be empty and I do not know when they will have data in them.

Abandon the volatile INDIRECT function and use the INDEX function for the termination of the range with MATCH locating the last column. The row can be determined by a MATCH on column A to "Label".
As I understand it, you want to locate "Label" in column A and have its location define the row in =INDIRECT("Input!$C$23:"LastColumn&"$23") where 23 currently defines the row.
Abandoning INDIRECT, the Input!$C$23 reference can be changed to:
=INDEX(Input!$C:$C, MATCH("Label", Input!$A:$A, 0))
So all that is left is to find the last column. There must be something on that worksheet that can be used to locate the last column regardless of column deletions or additions but I will just use the last value in the row that contains "Label" in column A.
'this finds the last text value in row 23
=MATCH("zzz", Input!23:23)
'this finds the last number or date value in row 23
=MATCH(1e99, Input!23:23)
'since I do not know whether *the row* (e.g. 23:23) contains text, numbers, dates
'or a combination of any of those, I will have to double up the formula and add error control
=MAX(IFERROR(MATCH("zzz", Input!23:23), 0), IFERROR(MATCH(1e99, Input!23:23), 0))
You can stitch two INDEX functions together with a colon just as if you were typing C23:Z23.
=INDEX(Input!$C:$C, MATCH("Label", Input!$A:$A, 0)):INDEX(Input!$A:$ZZ,MATCH("Label",Input!$A:$A,0),MAX(IFERROR(MATCH(1E+99, INDEX(Input!$A:$ZZ,MATCH("Label",Input!$A:$A,0),0)),0),IFERROR(MATCH("zzz",INDEX(Input!$A:$ZZ,MATCH("Label",Input!$A:$A,0),0)),0)))
Yes, it looks complicated but a lot of that comes from not knowing whether the row in question contains text or numbers and it does less work than a volatile INDIRECT (which recalculates whenever anything in the entire workbook changes) while remaining up-to-date despite row and column deletion/insertions.
BTW, when using text-as-cell-references within INDIRECT there is no need to use the $ absolute reference indicator. An address-as-text is a text string; it will not change no matter what you do to it short of retyping it. The above method does require absolute cell referencing as it is using actual cell and cell range references.

Related

Index Match Formula Keeps Continuing

I'm trying to create a dashboard, where upon selecting a company from a dropdown list, it will list all of the company's bankers where the data is on another worksheet tab.
So far, the Index Match formula seems to be running fine, however, it keeps on running and lists the bankers from other companies. How can I fix this?
File can be downloaded here: https://drive.google.com/file/d/1fw7yh4AEF1vx3cuG6E_1RBTrTD_k74KD/view?usp=sharing
You can try below approach by pasting below formula in cell E5:
=IFERROR(INDEX(RAW!$D$2:$D$279,SMALL(IF(RAW!$B$2:$B$279=$C$5,ROW(RAW!$D$2:$D$279)-1,10^10),ROWS($A$1:A1))),"")
This is array formula so it needs to be committed by CTRL+SHIFT+ENTER and then copy it down as much needed.
Explanation:
INDEX is retrieves the range reference from the defined GRID based on the row and column reference.
In this particular case, we intend to extract the references sequentially. This is performed by SMALL(IF(RAW!$B$2:$B$279=$C$5,ROW(RAW!$D$2:$D$279)-1,10^10),ROWS($A$1:A1))
Within this formula, first portion is the ARRAY building IF formula as below
IF(RAW!$B$2:$B$279=$C$5,ROW(RAW!$D$2:$D$279)-1,10^10) which tests cells in column B if they match criterion value. If the value matches then ROW reference is returned and if it doesn't then it returns a very large value i.e. 10^10. We subtract 1 from row reference as the data starts from row 2 so it should be (n-1) in principle. This arrangement produces an ARRAY like {1;2;3;4;5;6;7;10000000000....} for value "Acorda".
But we need to pass one element at a time to INDEX formula so this is done by SMALL(IfBasedArray,k) where k part is serially generated by ROWS($A$1:A1) which starts with 1 and increments downwards as the formula gets copied which thus enables INDEX to return desired results.
Finally, when the matches are finished then big number i.e. 10000000000 gets fed and generates an error which then is covered by IFERROR formula to return blank!
Please apply this formula to your sheet's E5 and copy down. Note that I created the named range Coms which refers to RAW!B2:B279.
=IFERROR(IF(INDEX(Coms,MATCH($C$5,Coms,0)+ROW()-ROW(E$5))=$C$5,INDEX(Coms,MATCH($C$5,Coms,0)+ROW()-ROW(E$5)),""),"")
If you prefer, the name in the formula can be replaced with the address, as you had it originally, or you define the named range to expand dynamically.
A simpler option than the 2 previous answers (doesn't require an CSE array entered formula or multiple nested INDEX/MATCHES) would be to have a couple of 'helper' cells in say G5/G6
Start Row would be =MATCH(C5,RAW!B:B,0) This finds the first row on your RAW sheet where the selected company appears
Advisor Count would be =COUNTIFS(RAW!B:B,C5) counts how many rows there are for this particular company
Then your formula in cell E5 becomes =IF(D5<=$G$6,INDEX(RAW!$D:$D,$G$5+D5-1),"") which you can drag down.
screenshot showing formulas/result

How to 'lock' a cell reference so it doesn't change when sorted/filtered?

I have a formula that relies on the values in other specific cells to work correctly, yet have found out that when I sort my worksheet randomly these references are replaced with relative references, or absolute ones if I use $ in the cell numbers. I have tried to 'name' the cells but naming them doesn't make a difference. Is there a way to uniquely identify a cell so I can find it no matter if the sheet is sorted or changed?
If I understand correctly, you want the selected range to stay the same regardless of the cell the formula is run from.
This is called absolute cell reference.
To do this, put dollar signs in front of the row or column you want to stay the same. In your example, using COUNTIF($M$100:$M$105) will select the range M100:M105, even when you copy that formula to a different cell.

Why won't my spreadsheet equations stay the same after i add a new row

I am making a spreadsheet which has multiple sheets and one of the sheets has an equation that looks to see if there is anything written in a certain cell on all the other sheets. Right now I just use this equation to find out whether or not it has anything written in that cell.
=IF(LEN('A'!N18)>1,CONCATENATE('A'!C18," "),)
This is just checking to see if anything is in the cell. The problem is that I want this to keep working if I add a new row on A, but right now it bumps row 18 to 19 and I am left with no row 18 so it won't even check that new row!
If you want to add a row between row 1 and row 18 on worksheet A but keep your formula references to A!N18 and A!C18 then you need to use INDIRECT or INDEX. Of these two, INDEX is the better choice as it is not volatile. A volatile function like INDIRECT will recalculate whenever anything in the workbook chanmges. A non-volatile function like INDEX will only recalculate when something that affects its outcome is changed.
'INDIRECT method; works but not optimal
=IF(LEN(INDIRECT("'A'!N18"))>1,CONCATENATE(INDIRECT("'A'!C18"," "),)
'INDEX method; works and is non-volatile
=IF(LEN(INDEX('A'!N:N, 18))>1,CONCATENATE(INDEX('A'!C:C, 18)," "),)
The link that Slai posted in the comments to your question should be what you're looking for. If you don't want a reference to change when you add/delete rows/columns you need to use absolute references. As currently written "N18" and "C18" are relative references. If you change the columns/rows on sheet 'A', these references will automatically change with them. That's why it's moving to row 19 on you.
You can turn these into absolute references by adding "$" like this: "$C$18" and "$N$18". The first "$" sets the absolute reference for the column and the second sets the absolute reference for the row. You can mix and match these for various results.
Mixing relative and absolute values really comes in handy when you are reusing a formula with slight differences. For instance, if you want to multiply a number in column b (starting with row 4) by the number in A1 and show the result in column C (also starting with row 4). You'll always be using A1 so we can set this as an absolute value and in C4 enter the formula =$A$1*B4. Copy this down column C and it will automatically update the B value to the new column but will always use "$A$1" for the other part.
You can split the relative reference by only using the "$" on the column or row reference of the reference. Whichever one you use it on will be locked and the other can still adjust based on changes to the sheet or copying. Learning how to use absolute references can be a great time-saver.
I'm curious about part of your formula though. Why are you concatenating C18 with just a blank space? If you are using the info somewhere else, it may make sense to add the space in that concatenate but it's a personal choice.

Excel Performance - INDEX-MATCH combination

I am using excel to create data sets that are used in a vba application later. I am using this formula:
=INDEX(BaseData!$L$2:$L$10000;MATCH(DataSet!D5&DataSet!E5&DataSet!K5;INDEX(B‌​aseData!$B$2:$B$10000&BaseData!$C$2:$C$10000&BaseData!$D$2:$D$10000;0);0))
usually with a range from f.ex.: A2 - A10000, because my data can be differently long and often vary in data selection.
However, this slows my excel extremely down. I switched to manual calculations, but then, when activating automatic again, my excel instance takes extremely long and often crashes.
I also tried to past some data, but when creating a new dataset, I have to pull the formula down again and sometimes through this errors occur in my data set.
Any suggestions what I can do to make the INDEX-MATCH formulas more performant?
I appreciate your replies!
UPDATE
I guess a lot of performance goes away because index-match does not select the exact range, but also counts in blank rows. How to get the exactl range with index match automatically?
As I mention in my comment above, as long as this is a 'regular' formula and not an Array Formula, you may find success simply replacing "A1:A10000" with "A:A". However barring that, you can create a cell which will calculate reference the number of rows of data which you have, and then use that cell to indirectly reference the complete column with data in it.
CALCULATING YOUR DESIRED RANGE
For the following example to work, I assume that: Column A includes an index key in the form of numbers only; Column A includes no numbers in the header and above; and that the index rows are continuous, with no breaks. Start with the following formula:
=COUNT(A:A)
If my assumptions above hold, then this will return the number of data elements in your table. Once we know where this data starts, we can use this COUNT to determine where it ends. Assume your header is in row 2. (I like to include the header so that if you insert a row beneath the header, Excel picks up that you want to include the new row in your formulas).With that in mind, this formula will create the Excel-style reference which finds the last cell in column A which has data in it:
=ADDRESS(ROW(A2)+1+COUNT(A:A),COLUMN(A2),1,1)
Assuming 50 rows of data [which start at row 3, below the header], and all other assumptions above, this formula will return the text result "$A$53".
If you wanted to do the same thing, but instead return the full range in Column A where data exists (from the header to row 53), you could do as follows:
=ADDRESS(ROW(A2),COLUMN(A2),1,1)&":"&ADDRESS(ROW(A2)+1+COUNT(A:A),COLUMN(A2),1,1)
This returns the text string result "$A$2:$A$53", which is a reference to the full index of unique ID values. It will automatically move around as you would generally expect if you insert any rows or columns. Now assume for your INDEX that you want to pull the same data, but for column B, instead. The formula will be exactly the same, except that where I have "COLUMN(A2)" above, replace with "COLUMN(B2)".
REFERENCING YOUR CALCULATED RANGE
So now you have the address of your proper, limited columns - but how do you actually reference those areas in a formula? By using the INDIRECT function. INDIRECT says "Evaluate some specific criteria. It will create a cell reference. Now look at that cell reference." In its simplest form, this could look like this:
=INDIRECT(A1)
Assume that A1 holds the value "B5". Indirect will pick up the value "B5", and instead of displaying "B5", it will go to B5, and pick up the value there. So to use this with the above, wrap the whole thing in the INDIRECT function. Instead of picking up the text string "$A$1:$A$53", it will actually now reference that range properly. Like so:
=INDIRECT(ADDRESS(ROW(A2),COLUMN(A2),1,1)&":"&ADDRESS(ROW(A2)+1+COUNT(A:A),COLUMN(A2),1,1))
USING A NAMED RANGE
But that is a very long formula to have, and you won't want to use it within a specific cell for a simple INDEX/MATCH. Instead of entering these formulas in cells (although you could), I recommend you go to the Formula Ribbon -> Name Manager -> New Name. Call the name for the index of A "ID_COLUMN". Call the name for the index of "B_COLUMN" (or something more specific).
FINAL FORMULA
Now, if you wanted to make an INDEX/MATCH of your table, which automatically grows/shrinks as you change the data, your formula would look like this [this would, for example, pick the row from column B where column A has the number 100]:
=INDEX(ID_COLUMN,MATCH(100, B_COLUMN, 0))

How to reference cell within '' worksheet title

I have the names of the tabs/worksheets (M-61,M-62,M-63W) at the top row (A1, B1, C1...etc)
I am trying to get a sum of several cells within the different sheets:
=SUM('M-60'!H21,'M-60'!H43,'M-60'!H86,'M-60'!H87,'M-60'!H97,'M-60'!H98)
However, right now I’m referring to the sheet itself, and have to apply the same formula to all the other sheets. This will require me to manually go and change all the sheet titles accordingly.
I was wondering if there is any way to reference the top row with the sheet titles within the formula so it automatically refers to the row text instead of me having to manually change the sheet title.
Edit
Now i got the reference to work, just wondering how would I do a sum of several cells in that tab
=INDIRECT("'"&$F1&"'!H87",TRUE)
Maybe:
=SUM(INDIRECT("'"&C1&"'!H21"),INDIRECT("'"&C1&"'!H43"),INDIRECT("'"&C1&"'!H86:H87"),INDIRECT("'"&C1&"'!H97:H98"))
(though there may well be a much smarter way).
You can use the INDIRECT function, which uses a string as an argument and converts it to a range. So
=M-60'!H21
is the same as
=INDIRECT("M-60'!H21")
or, if Sheet name is stored in, say, cell C1:
=INDIRECT(C1&"'!H21")
Your example has a SUM, though, which requires some adaptations. This your example:
=SUM('M-60'!H21,'M-60'!H43,'M-60'!H86,'M-60'!H87,'M-60'!H97,'M-60'!H98)
Since you are not using a range, you can convert that SUM into simple addition. Assuming Sheet name in cell C1
=INDIRECT("'"&C1&"'!H21")+INDIRECT("'"&C1&"'!H43")+INDIRECT("'"&C1&"'!H86")+INDIRECT("'"&C1&"'!H87")+INDIRECT("'"&C1&"'!H97")+INDIRECT("'"&C1&"'!H98")
This should solve your problem. More info here
By the way, if you were using a range, the OFFSET function with INDIRECT as an argument would work. But that's not necessary here.

Resources