Dynamic column change - excel

I am wondering if in Excel there is a way to change columns as described below?
=A1*(Formula that returns a letter)2
so for example the formula returns the letter F the result would be
=A1*F2
then if in cell A1 = 3, and F2 = 7. then where the Excel formula is the result would be 21.
I am hoping this approach is possible as I am hoping to use it in a SUMPRODUCT(SUMIFS(.......................................)) where the data I am analyzing is in a mess of columns and labels. and am confused with what else I can try.

The INDIRECT function can take text-that-looks-like-a-cell-reference and convert it into a valid cell reference.
=INDIRECT("A2")
=INDIRECT("A"&2)
=INDIRECT(<formula that returns a letter>&2)
=INDIRECT(<formula that returns a letter>&"2")
... each of these is the the same as,
=A2
All you have to do is pass along some constructed text string that can be interpreted as a cell reference into INDIRECT.
Caution should be exercised when using the INDIRECT function as it is considered a volatile¹ function. If they are used in calculation-intensive formulas or very prolifically, the user will experience calculation lag whenever anything in the workbook changes.
¹ Volatile functions recalculate whenever anything in the entire workbook changes, not just when something that affects their outcome changes. Examples of volatile functions are INDIRECT, OFFSET, TODAY, NOW, RAND and RANDBETWEEN. Some sub-functions of the CELL and INFO worksheet functions will make them volatile as well.

Related

Why use INDIRECT in place of direct reference in excel

Why would one use INDIRECT(cell) instead of a direct reference to cell?
Eg, I see a sheet where there are many references
A B C
1 SHEET1 B1 =INDIRECT("'"&A1&"'!"&B1)
2 SHEET1 B2 =INDIRECT("'"&A2&"'!"&B2)
3 SHEET1 B3 =INDIRECT("'"&A3&"'!"&B3)
Why not just
A B C
1 =SHEET1$B1
2 =SHEET1$B2
3 =SHEET1$B3
Indirect vs Direct Cell Reference
does not generally auto update vs does update
example adding or removing columns
arithmetic to change row or column vs what you type is what you get
example indirect("A"&3+5) vs =A3+5 is totally different
If you want to organize your formula references and change them all on the fly it is easier with indirect (although even easier just using the naming feature) but the real reason you "need" indirect is how else are you going to change the reference in your formula without manually typing it (answer: indirect)?
use case - programmatically list and loop a range of worksheets:
sheet references using formulas and values useful for addressing cells at scale.
Build references to many different cells, worksheets or workbooks that follow a logic
The most common use of the INDIRECT function is probably when someone wants to reference to many cells which cell references follow a logical rule. For example, assume you have an Excel workbook with hundreds of sheets, one for each day and the sheet names are the dates. Now you would like to summarize some of the values in those sheets on an overview sheet. In this case, you can type in your starting date, drag it down to your final date (Excel will increment the dates). Using the INDIRECT function, you can now easily build up the references within seconds.
However, consider that INDIRECT is a volatile function which will slow down your workbook. Further, if you insert rows/columns, the INDIRECT function won’t adapt. It get’s even worse when you reference to external sheets. Since INDIRECT updates it’s value with every change in the workbook, you will get #REF errors as soon as you close the referenced sheets.
I personally avoid INDIRECT for such cases (either using VBA or by choosing a different design for my workbooks, so no INDIRECT function is necessary)
Lock a cell reference
If you have a cell reference like let’s say =A10, Excel will always adapt the reference when you insert new rows or columns (if you for example insert a row above row 10, the reference changes to =A11). You can use the INDIRECT function in order to always keep the absolute cell reference: =INDIRECT(“A10”).
With named ranges
INDIRECT can be handy with named references. Have a look at the example where you have three named ranges:
NorthAmerica: B2:B5
Europe: C2:C5
Asia: D2:D5
You can now combine the INDIRECT function with many other Excel functions like SUM, MIN, MAX and so on. In the example, the drop down selection in G1 is referenced using INDIRECT to perform the calculation for the selected range.
Dynamic dropdowns
A similar example where you can use the INDIRECT function are dynamic drop downs. In this example there are two named ranges:
Fruits: A2:A4
Vegetables: B2:B4
In cell D3, there is a dropdown where you can select “Fruits” or “Vegetables”. In E3, we have a dynamic drop down with the source =INDIRECT($D$3). If you choose “Fruits” in D3, you will have a list with the fruits in the drop down.
So, there are definitely some things where INDIRECT might be an easy solution. But as I said, it is a volatile function that locks the cell reference. In most cases you can find different, better solutions. The main reason people use it is probably the lack of knowledge of better alternatives. In addition, I assume that the average Excel user is not aware of possible problems you might run into when using INDIRECT.
Indirect is very useful with Tables. For example, I create a table tblFindings with 10 rows. Then I assign the list to =Indirect("tblFindings"). Now I add 5 rows to the table, the dropdown list automatically updates.

Excel SUMIFS - define sum_range by dynamically changing column

I would like to make a SUMIFS formula where I can change the sum_range parametr dynamically on the grounds of this formula where I get the column: SUBSTITUTE(ADDRESS(1;MATCH("aaa";1:1;0);4);1;"")
In other words, I want to replace B:B in this formula =SUMIFS(B:B;A:A;"abc") with the formula above. But I am not able to combine those...
I found one solution here: https://stackoverflow.com/a/25814571/10452645
but I'm not quite satisfied with it. Is there another possibility to solve this task by combining SUMIFS and ADDRESS formula.
In the above example, you can find the sum of abc from column aaa using one of the three formulas:
=SUMPRODUCT((B2:D13)*(B1:D1="aaa")*(A2:A13="abc"))
or
=SUMIFS(INDEX(B2:D13,,MATCH("aaa",B1:D1,0)),A2:A13,"abc")
or
=SUMIFS(INDIRECT(ADDRESS(2,MATCH("aaa",A1:D1,0))&":"&ADDRESS(13,MATCH("aaa",A1:D1,0))),A2:A13,"abc")
You can replace aaa and abc with a cell reference so you can "dynamically" change the SUMIFS criteria.
Please note, as mentioned by #ScottCraner
ADDRESS and INDIRECT are volatile and should be avoided. INDEX is the quickest most solid method.
Reason being a Volatile Function is one that causes recalculation of the formula in the cell where it resides every time Excel recalculates. This occurs regardless of whether the precedent data and formulas on which the formula depends have changed, or whether the formula also contains non-volatile functions. It means it could potentially slow down the calculation of your Excel workbook if it gets complicated overtime.
Having that said, choose the function that suits your preference.

Using a SUMIFS formula to select the same column in multiple tables in excel

I am trying to shorten my formula a little and having a hard time figuring out the proper method to do so. I am trying to select certain cells in multiple tables to produce a single total. My code is this:
=SUMIFS(TransactionsChase[INFLOW],TransactionsChase[DATE],">="&Dec,TransactionsChase[DATE],"<"&DecPayChk2,TransactionsChase[CATEGORY],"<>"&"From*")
+SUMIFS(TransactionsPatelcoChecking[INFLOW],TransactionsPatelcoChecking[DATE],">="&Dec,TransactionsPatelcoChecking[DATE],"<"&DecPayChk2,TransactionsPatelcoChecking[CATEGORY],"<>"&"From*")
+SUMIFS(TransactionsPatelcoMM[INFLOW],TransactionsPatelcoMM[DATE],">="&Dec,TransactionsPatelcoMM[DATE],"<"&DecPayChk2,TransactionsPatelcoMM[CATEGORY],"<>"&"From*")
+SUMIFS(TransactionsCash[INFLOW],TransactionsCash[DATE],">="&Dec,TransactionsCash[DATE],"<"&DecPayChk2,TransactionsCash[CATEGORY],"<>"&"From*")
I would love to simplify it if possible into one sumifs statement. Any ideas?
If you apply the four table names within the a SUMIFS function with a volatile INDIRECT¹ function then wrap the whole thing in a SUM function and finalize it as an array² formula, the formula can be shortened visually but not calculation-wise.
In the following image, your original formula is in J2. The revised formula is J3 as,
=SUM(SUMIFS(INDIRECT(N$2:N$5&"[INFLOW]"),
INDIRECT(N$2:N$5&"[DATE]"), ">="&Dec,
INDIRECT(N$2:N$5&"[DATE]"), "<"&DecPayChk2,
INDIRECT(N$2:N$5&"[CATEGORY]"), "<>From*"))
Results should similar to the following. Note the minor improvement made to the , "<>From*" criteria. The table names could also be written out longhand. Instead of N$2:N$5 as,
{"TransactionsChase", "TransactionsPatelcoChecking", "TransactionsPatelcoMM", "TransactionsCash"}
As you can see from the sample image above, this formula will survive tables of varying row length. The only question that remains would be 'Is it worth it?'
¹ Volatile functions recalculate whenever anything in the entire workbook changes, not just when something that affects their outcome changes. Examples of volatile functions are INDIRECT, OFFSET, TODAY, NOW, RAND and RANDBETWEEN. Some sub-functions of the CELL and INFO worksheet functions will make them volatile as well.
² Array formulas need to be finalized with Ctrl+Shift+Enter↵. Once entered into the first cell correctly, they can be filled or copied down or right just like any other formula. See Guidelines and examples of array formulas for more information.

How intelligent is excel when is "parses" a function in a cell that includes identical calls to the same function more than once

Consider this: Cell C1 has a formula that calls another function many times:
eg =ExcelFuntionA(ExcelFunctionCallB(1), ExcelFunctionCallB(1)-1, ExcelFunctionCallB(1)-2, ExcelFunctionCallB(1)*3)
Note that the parameter(s) passed to ExcelFunctionCallB have the same value(s) each time.
If ExcelFunctionCallB(..) is a function that involves significant processing, would excel process the formula quicker if the result of ExcelFunctionCallB(..) was found in anther cell and the above formula was changed to reference this cell 4 times.
eg
Say A2 has the formula ExcelFunctionCallB(..)
C1 would then read =ExcelFuntionA(A2, A2-1, A2-2, A2*3)
I'm wondering how intelligent excel is!
Can is see that calls are identical and only process a call to the function once?
Is it intelligent enough to do this for array formulas as well as normal function formulas?
If you call a function that has been written in VBA how does it work?
Bear in might that a VBA function could reference cells directly
that are not passed as parameters ( I know this is naughty, but it
happens, and there is only small chance that it might return a different
value even if the parameters that are used with it are the same.)
Following on from the above, is there a way of telling the code that a VBA function does not access any excel cells directly? (A bit like application.volatile I would imagine)
A more realistic example: I've actually been experimenting with formulas like this:
=MID($A2, FIND("#",SUBSTITUTE("#" & $A2 & "#","#","#",B$1)), FIND("#",SUBSTITUTE("#" & $A2 & "#","#","#",B$1+1)) - FIND("#",SUBSTITUTE("#" & $A2 & "#","#","#",B$1)) - 1 )
which is well documented in one of the proposed by not approved answers to SO question 24182334/splitting-text-columns
Note that this formula uses the text FIND("#",SUBSTITUTE("#" & $A2 & "#","#","#",B$1)) three times.
[PS. I know about split in VBA and the text to columns menu option.]
If ExcelFunctionCallB(..) is a function that involves significant
processing, would excel process the formula quicker if the result of
ExcelFunctionCallB(..) was found in anther cell and the above formula
was changed to reference this cell 4 times.
This is correct, referencing an already calculated cell is faster than recalculating the same thing multiple times in an individual cell. Excel will calculate each one separately
Also, as pointed out by pnuts, this link may be of interest: http://msdn.microsoft.com/en-us/library/office/ff700515%28v=office.14%29.aspx
(posted as answer at chris neilsen's suggestion)
The key text from the link in the above answer (here) is
If you have a calculation-intensive formula where you want the result to be shown as zero if there is an error (this frequently occurs with exact match lookups), you can write this in several ways.
You can write it as a single formula, which is slow:
In cell A1 =IF(ISERROR(time expensive formula),0,time expensive formula)
You can write it as two formulas, which is fast:
In cell A1 =time expensive formula
In cell B1 =IF(ISERROR(A1),0,A1)
Starting in Excel 2007, you can use the IFERROR function, which is designed to be fast and simple, and it is a single formula:
In cell A1 =IFERROR(time expensive formula,0)
So the same logic would apply to other types of formulas it would seem.

Prevent cell from being calculated

Is it possible to prevent calculations happening to a single cell in Excel sheet? Let's say I have 1001 cells that are very fast to calculate, but 1 cell slows sheet down, is it possible to disable calculations for that 1 cell only?
What I'm NOT trying to do:
Disabling all of cell calculation programically
Calculating specific cells programically while global calculation is set to manual
Use Excel's =IF() function. It is set up to "short-circuit" -- it only evaluates the second parameter if the first parameter is true, oppositely for the third parameter.
So, if the cell is C1, and the cell's formula is currently
=LOOKUP(2,1/(A1:A100000=666),B1:B100000)
and you want it to only be calculated when D1 is true, use
=IF(D1,LOOKUP(2,1/(A1:A100000=666),B1:B100000),C1)
Notice it's a circular reference -- it's how you keep the value the same when D1 is false. Turn on iteration if you want to get rid of the warning message.
Another way is to use one of the third-party Add-Ins out there that lets you store a global variable off-sheet and then retrieve it, which would use syntax like this:
=IF(D1,SetGlobal("C1StoredCalculation",LOOKUP(2,1/(A1:A100000=666),B1:B100000)),GetGlobal("C1StoredCalculation"))
SetGlobal() and GetGlobal() can also be written in VBA, though they'll be a tiny bit slower than an XLL, and they'll lose the value if you reset your VBA project.
Excel does not have a method to disable calculation for a single cell.
You could move the slow formula to a separate sheet and use worksheet.enablecalculation to disable calculation for that sheet.
Or you could store the formula somewhere as text, store the result as a value in the cell, then restore the formula when you want to calculate it.
You can use a replacement UDF and take advantage of a lack of volatility.
Say we have a formula like:
=LOOKUP(2,1/(A1:A100000=666),B1:B100000)
Excel will re-calculate this if any cell in cols A or B change, but the UDF
Public Function myudf(r As Range) As Variant
myudf = Evaluate("LOOKUP(2,1/(A1:A100000=666),B1:B100000)")
End Function
will only be re-calculated when its argument changes. So pick a cell and enter:
=myudf(Z100)
make any changes you want to cells in cols A or B and myudf will remain un-re-calculated until you change Z100
You can use the same tiny trick to make "quasi-volatile" versions of =TODAY() or =NOW() for =RAND()
I don't think this can be done. You can turn off automatic calculation in entire workbooks (as you mentioned), but I don't think there is a way to do this on an individual cell.

Resources