MS Excel passing ranging in search function - excel

Can anybody explain why this works,
=LOOKUP(1E+100,SEARCH($O$2:$O$5,J6),$O$2:$O$4)
this looks up a value in a cell from a range of cells, works as intended.
But the search(O2:O5,J6) does not work on its own?
so, SEARCH($O$2:$O$5,J6) returs #value err. I would think that this would return the position if found. If I pass in {""}, e.g. Search({"xyz","zyx","xsy"},J6) this works and makes sense but why doesn't a range work.
an explanation would be greatly appreciated.

The answer to your question is two-fold.
Firstly, most functions won't accept multiple values for a single argument. Getting them to do so requires that they be calculated as array formulas. You can force a function to do this manually by confirming it with CTRLSHIFTENTER (instead of just ENTER). You'll know it's entered as an array formula because curly braces {} will surround the formula in the formula bar. Once you've done that, go to the Formulas Tab -> Evaluate Formula to see the steps. With just the Search function, you'll probably get something like {#N/A!;#N/A!;11;#N/A!;#N/A!;}. Notice that it is an array (a list) of results from the formula, with most being errors (because the text wasn't found) and one with a proper result.
This brings us to the second part of the answer, the error-handling. Again, most functions don't handle errors well. If any part of a function errors, the whole function results in that error. You can get around this with error handling functions like ISNUMBER or IFERROR.
So now that we know that, why does LOOKUP work? Because it handles both cases as part of its built-in functionality. Lookup, as part of its built-in functionality because of the specifics of how it works, evaluates its arguments as arrays This means that if you feed it another function as an argument (like SEARCH for example), it will force that function to be evaluated as an array.. A few other functions also do this, like SUMPRODUCT and INDEX.
Secondly, LOOKUP already has built-in error handling. If there are errors within its arguments, it will return the answer closest to the first argument (but still underneath it). This is why the first argument in your provided LOOKUP function is 1E+100 which means 10^100 or basically a 1 followed by 100 zeroes. Any search result found is going to be a number smaller than that, so if it finds anything, it will return that number, because everything else will result in #N/A! and LOOKUP will ignore those errors.

Related

Efficiently replace 0 with NA

I am trying to find a way to efficiently replace zero with NA() in an Excel formula. I know the following works:
=IF(FORMULA = 0, NA(), FORMULA)
But my problem is that this will cause FORMULA to execute twice. I have cases where this may be a longer =SUMIFS() in a giant table.
So I would like for:
No VBA
Only have the base FORMULA calculate once
I thought at first to try to use SUBSTITUTE() to replace "0" with something that would trigger a value error, and then just wrap all of that within IFERROR(). That obviously fails since SUBSTITUTE() cannot be forced (to my knowledge) to check for full word match (so 100 would trigger the error).
Is this possible? I have thought for years it was not, but decided to put some thought back into it.
The general answer is
=IFERROR(f'(f(FORMULA)), AlternateValue)
where f(FORMUALA) returns an error (any error will do) for values of FORMULA that you want an alternate value for.
And f'(...) is the inverse of f(...), so f'(f(FORMULA)) returns FORMULA for other values.
Ensure the first function is applied to the whole of FORMULA. Enclosing it in () guarantees that.
Secondly, ensure the two functions are applied in the correct order, also achieved using ().
In this case you want an alternate value for 0 so you can use
=IFERROR(1/(1/(FORMULA)), NA())
Calculation and display can occur in two different locations - why merge operations when you don't need to?
A1 - =Formula
B1 - =If(A1 = 0, NA(),A1)

Index Match Works on some cells, not others

I was using this Index Match formula with no issues:
=INDEX('Rain Data For 9 Stations'!A:S,MATCH(RainWICSProximity!J100,'Rain Data For 9 Stations'!A:A,0),INDEX($N$4:$N$12,MATCH(H100,$M$4:$M$12,0)))
I added more data, and it now only returns some values, while returning #N/A for others, even though there is a value to return.
Index returns the value in a range.
What you are doing is =INDEX(MATCH(),INDEX(MATCH())). It works due to some luck, because sometimes the second Index() returns cell with value as well. However, if the second index returns cell with an empty value, then the first index has to return something like =Index(4,0), which is #N/A.
In general, try =Index(Match(),Match()).
To see where exactly the error is, select the cell with the formula, go to the Excel ribbon >Formulas>Evaluate Formula.
Then press a few times Evaluate Formula and see what happens:
See this answer for step-by-step formula evaluation.
#Vityata was correct, Index, Match, Match works wonderfully, also, my original formula does work.
The issue was, I had calculate set to Manual, not auto, in excel settings.
I believe you need to expand your range. I am not real familiar with Index Match but trying to learn to use it more, but I believe it is kind of like VLOOKUP. Your ranges $N$4:$N$12 and $M$4:$M$12 is where it is looking right? If so, those ranges are not expanding even though you added more data. So you need to expand it to like $M$4:$M$100 or whatever. Or expand it to find the last row which is what I usually do. like mine would be "$M$4:$M" & LastRow & "" or something like that.

LOOKUP function not giving expected results

I am having a problem with the LOOKUP function.
To test some things, I entered the following function
=LOOKUP(4.19, $B$2:$B$6, $C$2:$C$6)
And on every line, it gives me a different answer. I don't know what is the problem with it because it just gives me the wrong answer. It should say "blue".
The lookup value must be (always) in the first column of the lookup table. So, if you are looking for 4.19 (which is in the right-most-column) in order to return a value from a column further to the left then you'll have to change to INDEX/MATCH like so:
=Index($C$2:$C$6,Match(4.19, $B$2:$B$6,0))
With LOOKUP the values you are searching in must be in descending order. If you want to keep the order as is, you might want to use INDEX/MATCH functions.
With the third argument you tell the function what the output should be if the first argument( 4.19) is valid. since you enterd a range of cells, the output differs. Also if you are searching for an exact value you should add a further argument at the end of the function
=VLOOKUP(4.19, $B$2:$B$6, D1, FALSE)

How to return index of an array in a user defined function in excel?

I have a user-defined function (not mine, in a plug-in) which returns an array of values in column A. I also have the following (array) formula in column B, which returns all the cells in column A which contain some specified string:
=INDEX($A$1:$A$256,SMALL(IF(ISNUMBER(SEARCH($B$1,$A$1:$A$256)),ROW($A$1:$A$256)),ROW(2:2)))
This uses the ROW() function to get the row(s) of the matching elements. This all works fine.
However, I would like to clean up my sheet a little, and do something like the following:
=INDEX(UDF(),SMALL(IF(ISNUMBER(SEARCH($B$1,UDF())),ROW(UDF())),ROW(2:2)))
Youll notice I have replaced the array '$A$1:$A$256' with the returned array from my UDF. This seems to break at the ROW(UDF()) part. Is there a similar function, which return the index of the array, or 'row'?
NOTE: I would rather not do this in VBA, and I cannot edit the UDF (it is not mine).
EDIT:
OK I have a bit more information. If I do as suggested in the comments, and evaluate the formula, all the evaluation returns is the first element of the array. This brought up a related, second question. How does one do this with a UDF? If you are familiar with Bloomberg, you will know that when you call some BDH or BDP formula (NOT calling it as an array formula), it fills a range with values. How does this happen?! I feel like if i know the answer to this question, it will help me understand what is happening in this particular UDF.

ISNUMBER statement only reading first condition

Good day,
I am trying to insert this formula into excel to search for specific terms in a cell, and return a value "50" if the term is found.
I have been able to successfully implement this using the below primitive formula:
=IF(C1394<>70,C1394,IF(ISNUMBER(SEARCH("*PD*",B1394)),"50",IF(ISNUMBER(SEARCH("*OD*",B1394)),"50",IF(ISNUMBER(SEARCH("*OC*",B1394)),"50",IF(ISNUMBER(SEARCH("*OF*",B1394)),"50",IF(ISNUMBER(SEARCH("*PC*",B1394)),"50",IF(ISNUMBER(SEARCH("*MS*",B1394)),"50",C1394)))))))
I tried to make my formula more efficient and dynamic using the below approach, however excel only reads the first condition "PD" and ignores the rest
=IF(C8266 =70,IF(--ISNUMBER(SEARCH({"*PD*","*OD*","*OC*","*OF*","*PC*","*MS*"},B8266)),"50",C8266),C8266)
Can someone please advise what am I doing wrong?
You're just missing an OR clause, see this: http://www.mrexcel.com/forum/excel-questions/601195-check-multiple-text-strings-cell.html#post2977162
So you want:
=IF(C8266=70,IF(OR(ISNUMBER(SEARCH({"*PD*","*OD*","*OC*","*OF*","*PC*","*MS*"},B8266))),"50",C8266),C8266)
What you are doing wrong is you are trying to evaluate an array within a formula that doesn't usually evaluate arrays. You evaluate such formulae by pressing and holding Ctrl+Shift and then press Enter.
Otherwise, your formula can be shortened to the below:
=IF(OR(ISNUMBER((C8266=70)*SEARCH({"PD","OD","OC","OF","PC","MS"},B8266))),"50",C8266)
SEARCH does not require wildcards.
A boolean multiplied by a number gives a number. An error multiplied by a number gives an error. So you can safely combine the first and second checks together and drop the --.
OR is then used to check if there is at least 1 number within the array of numbers and/or errors.
Similarly array invoked (with Ctrl+Shift+Enter).

Resources