Searching for Text in Excel
I know it could be done in VB as a function so if you can give me that too, it'd be useful. Anyway, the thing that we want to accomplish: Let's say you want a formula that will detect the existence of text within another cell and if it exists, display some text. Let's also say that we can't use Autofilter for this. For instance, column B has the text to search and we want column A to show "YES" if the cell in column B has a certain text string ("BOOB") in it, otherwise display "NO". Excel has two functions SEARCH() and FIND() that are good candidates. They return the position where the text exists in the searched cell. But there is one major problem: If the text is not found in the cell, the function returns #VALUE!. Excel can't evaluate an expression that returns #VALUE! any further so we can't do: [code:1] =if( SEARCH("BOOB", B1) = #VALUE!, "NO", "YES") [/code:1] I have no idea why Excel developers implemented the function this way. You think a return value of -1 would be more reasonable to detect if the string exists. This is what I would expect as a programmer anyway. Another argument against this approach is the very name "SEARCH" indicates (to me anyway) that the value [i]might[/i] not be there...so why should this function return an error? Anyway, the ISERROR() worksheet function saves the day here...basically we test the return value of the SEARCH function for an error: [code:1] =if( ISERROR( SEARCH("BOOB", B1) ), "NO", "YES") [/code:1] Tada! Note that I only recently discovered the ISERROR() function in Excel. Before I was doing the following hack : Insert a column 'C' after column 'B' that appends the search string to the original string. This has the following formula: [code:1] = B1 & "_BOOB" [/code:1] Now we test the search value of the contents of column 'C'. Since the string in column 'C' is [u]always[/u] present we will get some value here (i.e. we will never get an error). Now we can test the return value of the SEARCH() function and if it is greater than the length of the original string in column 'B' then we know the string was not found. The formula in column 'A' would be: [code:1] = if(SEARCH("BOOB", C1) > LEN(B1), "NO", "YES") [/code:1] Whew! Regards, Jeff

you're a boody guru.
Cheers, great man.

Post new comment
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can use BBCode tags in the text.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor, sorry but we need it to help prevent automated submissions.
eight plus five equals
Solve this math question and enter the solution with digits. E.g. for "two plus four = ?" enter "6".