Convert text formula of a function to a 'real' formula - excel

I have in cell A1 the formula VLOOKUP(A2;Table3[#All];3;FALSE). I am struggling to find a way to output the result of the previous formula in B1.
So apparently, you can calculate a text formula using the function "Evaluate" accessible only with VBA like so :
Function Eval(Ref As String)
Application.Volatile
Eval = Evaluate(Ref)
End Function
Then you're able to call it everywhere in your sheet for example, if you put 1+2 in cell A1 and =Eval(A1) in cell B1, you get output 3 in B1. So it works fines in this case!
Works well when you 'evaluate' a simple number too:
HOWEVER
I found out that Evaluate does not work on texts that include a function (like my above formula of Vlookup).
For example if you put Eval(A1) in A2 (where 'Eval' is a function itself) then put =Eval(A2) in B2, you get "#VALUE".
Same output with above formula.
Does anybody know how to calculate a text formula like : VLOOKUP(A2;Table3[#All];3;FALSE) (without having to add "=" by hand before the formula) ?
Thank you!

I'm not sure if it's documented in a more authoritative place - this link does mention it - but Evaluate requires a comma as the argument separator in formulas, regardless of your local setting.
As far as the VLOOKUP goes, either change the ; to , in the original text, or use SUBSTITUTE and Eval the result.

Related

Have COUNTIFS ignore all blank cells (= empty cells and cells containing "")

I want to get a formula with COUNTIFS, like
=COUNTIF(A1:A3,"<>"&"")
such that when A1 = 2, A2 = "", A3 = empty, it returns 1.
Notes:
A2 contains an empty string, as the result of a formula. A3 is a blank cell, with no formulas in it.
The formula posted returns 2.
I tried using various numbers of double quotes. I always get 2.
I tried using &CHAR(34)&CHAR(34). I get 2.
The solution posted in How do I get countifs to select all non-blank cells in Excel? is what I tried, it returns 2 (not useful).
The formula would actually be =COUNTIFS(range1,cond1,range2,cond2), that is why I cannot use something like
=ROWS(A1:A3)-COUNTIF(A1:A3,"") or =ROWS(A1:A3)-COUNTBLANK(A1:A3) (see this).
range1 and range2 would come from expressions with INDIRECT, but that is probably not relevant.
I have worked it out with =SUMPRODUCT(--(expression1),--(ISNUMBER(A1:A3))), but I am specifically asking about the possibility of using COUNTIFS. Discrimination of number vs. text (e.g.) is not relevant at this point.
Blank vs. Empty string is the source of "troubles" (see, e.g., this).
Excel itself is somewhat ambiguous with respect to the definition of BLANK. In my example, ISBLANK(A2) returns FALSE, but COUNTBLANK(A2) returns 1.
I am not interested in a user Function.
Use a SUMPRODUCT function that counts the SIGN function of the LEN function of the cell contents.
    
As per your sample data, A1 has a value, A2 is a zero length string returned by a formula and A3 is truly blank.
The formula in C2 is,
=SUMPRODUCT(SIGN(LEN(A1:A3)))
I was having this exact problem, and I just found out about the "?*" wildcard which searches for any one or more characters, thus avoiding the empty string problem--genius! See Jonathan Gawrych's answer (posted right after the selected answer) here:
Excel Countif Not equal to string length of zero
Not sure if this works for the OP, since it looks like the value in A1 could need to be handled as a number not a string, but it might help anyone else who arrived here looking for a text-parsing solution.
Is using SUM instead of COUNTIFS an option? If so, I've found it to be much more flexible for filtering data sets. For example:
=SUM(IF(NOT(ISBLANK(A1:A3)),IF(NOT(ISTEXT(A1:A3)),1,0),0))
(entered as an array formula). IF(NOT(ISBLANK(x))... filters out non-blanks, then IF(NOT(ISTEXT(x))... filters out non-text. Whatever survives the filters is counted by summing 1. You can add as many filters as necessary. If you wanted to filter out only empty strings but include other text entries you could use a filter like
IF(ISTEXT(x),IF(LEN(x)>0,1,0),0)

String argument to INDIRECT

I have an unwieldy cell argument along the lines of:
COUNTIFS(E1:E300,"5",$O$2:$O$300,"Apple")
I would like to use INDIRECT as follows:
COUNTIFS(INDIRECT("E"&A1&":E"&A2),"5",INDIRECT("O"&A1&":O"&A2),"Apple") INDIRECT("E"&A1&":E"&A2))
Where A1 holds 1 and A2 holds 300. But again this is unwieldy, Ideally instead of A1 and A2, I would have a cell A3 containing. Similarly for A4
"E"&A1&":E"&A2
Then use
COUNTIFS(INDIRECT(A3),"5",INDIRECT(A4),"Apple") INDIRECT(A3))
Is this possible?
I can get it to work with a simple string like, but as soon as I add an & then it no longer works.
You don't say how 'it no longer works'.
Your logic and example works fine for me although I'm not sure what the final 'stray' INDIRECT statement is in your code lines?
If there is more than one criterion, COUNTIFS returns the count of the ANDed criteria not the sum of the counts.

Nested IFs in function not working

Hi, I'm trying to input a function in C2 in order to assign a numerical value to the minutes given in Column B. The criteria for this can be seen in the image above (starting at G1).
I have tried using a formula I copied from a similar situation but is not working:
=IF(B2<=$A$2,5,IF(B2<=$A$3,4,IF(B2<=$A$4,3,IF(B2<=$A$5,2,1))))
Any help would be appreciated, thanks
The suggestion by #Jeeped above will simplify the formulas needed. If you have to keep the 'A' and 'B' cells as they are listed above, you have to extract the number from the 'x min' format and convert the 'x' to a number so it can be compared (I assume a " " exists after the number. Could search for " min" as well).
=VALUE(LEFT(A2,SEARCH(" ",A2)-1))
Using the above, if A2 = '60 min', the formula will produce a '60'.
Now that the cell contents can be treated as numbers, the comparisons can be made. Formula for C2:
=IF(VALUE(LEFT(B2,SEARCH(" ",B2)-1))<=VALUE(LEFT($A$2,SEARCH(" ",$A$2)-1)),5,
IF(VALUE(LEFT(B2,SEARCH(" ",B2)-1))<=VALUE(LEFT($A$3,SEARCH(" ",$A$3)-1)),4,
IF(VALUE(LEFT(B2,SEARCH(" ",B2)-1))<=VALUE(LEFT($A$4,SEARCH(" ",$A$4)-1)),3,
IF(VALUE(LEFT(B2,SEARCH(" ",B2)-1))<=VALUE(LEFT($A$5,SEARCH(" ",$A$5)-1)),2,1))))
This is ugly, but works given the original question.
Try this formula (in this case for C2):
=SUM((B2<=$A$2:$A$5)*1)+1
It is important to use it as array formula. So after typing or inserting this formula to your cell don't just commit with Return but hit Ctrl+Shift+Return. If you did it right, your formula will be surrounded by curly brackets in formula bar:
{=SUM((B2<=$A$2:$A$5)*1)+1}

Comparing cells with using Excel LEFT Function

I wanted to compare value in cell A1 and B1.
However, sometimes PC give different value as cell B1.
Initial code I've been writing is as below in cell C1:
=IF(ISERROR(MATCH(A1,$B$1:$B$5,0)),"","Duplicate")
This code only compare exact value. Thus, I'm trying to add LEFT Function into function above:
=IF(ISERROR(MATCH(Left(A1,8),$B$1:$B$5,0)),"","Duplicate")
But above function only count character in A1 only. How do I add LEFT(B1,8) to also read value in cell B1?
Thanks.
Regards,
Zaiem
Maybe what you would like is:
=IF(MAX(IFERROR(FIND(LEFT(A$1,8),B$1:B$5),""))=1,"Duplicate","")
entered with Ctrl+Shift+Enter

Evaluating a formula defined in another cell

Say I've got a function name in cell A1, like SUM, and some data in B1 and C1. Is there any way to define a formula in one cell such that it calls the formula that is defined in A1 and have it work on B1 and C1 as data?
So something like:
=A1(B1:C1) should be equal to =SUM(B1:C1) since A1 contains the word SUM in it.
Essentially, something like preprocessor macros in C, or function pointers maybe.
You could do it using vba by creating a user defined function in a module:
Public Function applyFunction(functionName As Range, argument As Range) As Variant
applyFunction = Evaluate(functionName & "(" & argument.Address & ")")
End Function
If you put SUM in A1, and 1, 2, 3 in B1, B2, B3, =applyFunction(A1,B1:B3) will return 6. It is equivalent to calling =SUM(B1:B3).
EDIT
If you really don't want to use VBA, you can create a name (insert name in excel 2003 I think, Define Name in Excel 2010):
Define a new name (let's say eval1)
in the refers to area, enter =EVALUATE(A1&"(B1:B3)"), where A1 contains SUM and B1:B3 is the range with the numbers
in a blank cell, type =eval1 and it should return the result
But this approach is less flexible.
If you want to use a formula instead, you could possibly use the SUBTOTAL() function. However, it is a little limited.
Check out the image. It uses the reference to the function number for subtotal. You can expand this by creating a vlookup function if you want to use the name of the function, but you also have to provide a way to determine to use the regular function num or the 101-type values which ignores hidden values in the data range.
Check out this link for more info:
http://office.microsoft.com/en-us/excel-help/subtotal-function-HP010062463.aspx

Resources