I am not really sure how to explain it, I am trying to run one formula to search for information and use the result multiple times in a single cell. I am currently doing this to display the queried value AND then running the same formula to find the average also.
Example Formula (Simplified)
=<FormulaToFindValue> & " (" & Round(<FormulaToFindValue>/I52,2) & "/day)"
Acutal Formula
=SUMPRODUCT((MONTH('W.A.R. 2016'!$A4:$A369)>=7)*(MONTH('W.A.R. 2016'!$A4:$A369)<=9)*('W.A.R. 2016'!$A4:$A369<TODAY())*('W.A.R. 2016'!Q4:Q369)) & " (~" & IFERROR(ROUND(SUMPRODUCT((MONTH('W.A.R. 2016'!$A4:$A369)>=7)*(MONTH('W.A.R. 2016'!$A4:$A369)<=9)*('W.A.R. 2016'!$A4:$A369<TODAY())*('W.A.R. 2016'!Q4:Q369))/B18,2),0) & "/day)"
As you can see I have to use the same Formula two times in the same cell to get the result I want, is there a way to only run the Formula once and use the resulting value multiple times? Usually this would be done by storing the value in a variable but I can't don't see similar capability in excel.
Put the formula in a name (be careful to pick your relevant choice between fixed and dynamic references to the input ranges). Now that name can be used multiple times in a cell or in the workbook. In the example below the formula is the sum of the product between two fixed ranges and a nonsense formula in the shown cell uses this formula twice:
Related
This question already has an answer here:
Excel VBA - How to add dynamic array formula
(1 answer)
Closed 1 year ago.
I'm stumped and hoping for an answer to this weird issue.
In my VBA code, I'm creating a formula that access a column in my table called "Agent". (tbl_CSV_Import_Values[Agent]). The Agent column is in column 8 on my sheet. The table gets created/removed sometimes, and can have a different number of rows. After deleting the table, I recreate it, then run the VBA code to rewrite the formulas. For other formulas this works fine. But...
When I use the below code to stuff the formula into cell F3, it adds "#" to TWO of the "Agent" column References. For no reason I can discern. But it weirdly leaves one of the 3 Agent references alone. (See below)
My VBA:
ActiveSheet.Range("F3").Formula = "= ""Number of Agents: "" & SUM(IF(tbl_CSV_Import_Values[Agent]<>"""",1/COUNTIF(tbl_CSV_Import_Values[Agent], tbl_CSV_Import_Values[Agent]), 0))"
The Formula that get's place in cell F3 (generating an error due to those "#"s in front of Agent:
= "Number of Agents: " & SUM(IF(tbl_CSV_Import_Values[#Agent]<>"",1/COUNTIF(tbl_CSV_Import_Values[Agent], tbl_CSV_Import_Values[#Agent]), 0))
Of course, the formula fails... unless I simply delete those "#" signs from the formula manually. (The excel error says "A value used in the formula is a wrong data type".) With the two #'s deleted, the formula immediately works fine.
Does anyone have any idea why Excel is deciding to whack my column references (and only 2 of the 3?!) like that, and what I can do to stop it?
-Daniel
If the first parameter in IF (IF(tbl_CSV_Import_Values[Agent]<>"""",...) and the second parameter in COUNTIF (COUNTIF(..., tbl_CSV_Import_Values[Agent])) shall be a reference to a range of cells rather than a single cell, then the formula would must be an array formula. So in former Excel versions the formula would must be applied as an array formula by pressing Ctrl+Shift+Enter.
Since Excel 365 the decision whether array formula or not is tried to choose automatically. But when set via VBA using Range.Formula, it always sets a normal formula and not an array formula. And in a normal formula first parameter in IF and second parameter in COUNTIF cannot be column references but must be references to single cells. That's why the adding of the #.
To avoid this do explicitly set an array formula using VBA:
ActiveSheet.Range("F3").FormulaArray = "= ""Number of Agents: "" & SUM(IF(tbl_CSV_Import_Values[Agent]<>"""",1/COUNTIF(tbl_CSV_Import_Values[Agent], tbl_CSV_Import_Values[Agent]), 0))"
I am trying to use a simple IF formula in a cell but I get This is not a formula error.
This is the IF I am trying (C6 is empty):
=IF(C6>=70,"Pass","Fail")
What I really need is a formula that calculate multiplication between two cells or empty cells depending on value of one of the two cells.
Something like:
=IF(G12=1,D2*G12,"")
Excel can be tricky sometimes. If your excel cell formula does not work always check the following first:
do I have "=" in the first place
is every string within quotation marks " "
between every argument use ";"
maybe there is another language dependent command
I'm trying to write a formula that will allow me to count a number of lines used between a cell range. I created a test formula to see if my thought process was straight. For my test I put a header called "NUM" (A1) and then on the same column a bunch of rows of "1"'s then another header beneath the first array called "NUM2" (column A but could be anywhere) with more "1"'s under that as well. I'm using a formula to find the cell address to find the first header which is
=CELL("address",INDEX(A:A,MATCH("NUM",A:A,0),1))
which printed ($A$1) and then combining it with
=CELL("address",INDEX(A:A,MATCH("NUM2",A:A,0),1))
which printed ($A$14) to get the second header name, after that I'm using a COUNTIF function to count the ones like so:
=COUNTIF((CELL("address",INDEX(A:A,MATCH("NUM",A:A,0),1)))&":"& CELL("address",INDEX(A:A,MATCH("NUM2",A:A,0),1))),"1"))
and I thought that if I didn't use the formulas inside the formulas it would look like:
=COUNTIF($A$1:$A$14,"1")
which prints my desired number of 12. My question is can I not put the combination of the two formulas inside of the bigger COUNTIF statement?
You are just missing an INDIRECT which will turn your string into a cell reference.
=COUNTIF(INDIRECT(CELL("address",INDEX(A:A,MATCH("NUM",A:A,0),1)) & ":" & CELL("address",INDEX(A:A,MATCH("NUM2",A:A,0),1))),1)
A simpler version (assuming column A) would be
=COUNTIF(INDIRECT("A"&MATCH("NUM",A:A,0)&":A"&MATCH("NUM2",A:A,0)),1)
You could also use OFFSET.
For my Excel sheet, I'd like to create a column that searches a cell from a specific list of values and displays all the values that were contained in it.
I have a column with "instructions" that mention certain document names within them. In another column, I want to show all the document names that were detected from the instructions column. There is a separate column (not shown) that contains a master list of all possible documents, from which the search is based off of.
At the current moment, I have an extremely long formula:
=IF(ISNUMBER(SEARCH(Sheet2!F$3, I4)), Sheet2!F$3& CHAR(10),
IF(ISNUMBER(SEARCH(Sheet2!F$4, I4)), Sheet2!F$4& CHAR(10),
IF(ISNUMBER(SEARCH(Sheet2!F$5, I4)), Sheet2!F$5& CHAR(10), .... etc.
The biggest problem is that it only returns one of the values (Document names), in addition to being very long and inefficient.
I've also tried using:
=SUMPRODUCT(--ISNUMBER(SEARCH(F18:F20, H19)))>0
But this only returns a TRUE/FALSE and not all the values.
I'd like to be able to search using an array of the document column and return all the values that were contained in the "instructions" cells.
I've looked at multiple related questions on stackoverflow, but I couldn't find one that was able to link the different elements of multiple value search (from array and its containment in a cell), multiple value return (in a single cell). I definitely don't want to hard code the values into the formula either, so how would you approach this?
Use TEXTJOIN:
=TEXTJOIN(CHAR(10),TRUE,IF(ISNUMBER(SEARCH(Sheet2!F$3:F$10,I4)),Sheet2!F$3:F$10,""))
This is an array formula. It requires that one confirms it with Ctrl-Shift-Enter instead of Enter when exiting Edit mode.
TEXTJOIN was introduced with Office 365 Excel.
If you do not have Office 365 Excel then you can modify your long formula. Do not nest the IFs but concatenate them and put the CHAR(10) before each. like so:
=MID(IF(ISNUMBER(SEARCH(Sheet2!F$3, I4)), CHAR(10) & Sheet2!F$3,"") &
IF(ISNUMBER(SEARCH(Sheet2!F$4, I4)), CHAR(10) & Sheet2!F$4,"") &
IF(ISNUMBER(SEARCH(Sheet2!F$5, I4)), CHAR(10) & Sheet2!F$5,"") & ...,2,999)
Where the ... is the rest of the list.
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.