verify value in a Cell A exist in Cell B in Excel - excel-formula

I want to verify that value in cell A is present in Cell B. I used
=IF(ISNA(MATCH(A2,$B$2:$B$2,1)),"No","Yes") 'this working partially.
It is displaying No for the below cell values
Cell A Value:
Walk of Fame
Cell B Value:
While We're Young (2015)
Critics Consensus: Poignant and piercingly honest, While We're Young finds writer-director Noah Baumbach delivering some of his funniest lines through some of his most relatable characters. ... Noah Boaumbach's comedy While We're Young stars Ben Stiller and Naomi Watts as Josh and ...
what is wrong with this?

Try this formula
=IF(A2="","",IFERROR(IF(SEARCH(A2,$B$2:$B$2,1)>0,"Yes"),"No"))
The first IF checks for an empty search field, and the output field is empty if that is the case. The SEARCH looks for the position of the search string in a text. If it is found, it returns the start position but gives an error if the search string is not found. Therefore I surrounded it by an IFERROR to return a No if the text is not found.

If case doesn't matter:
=IF(ISERROR(SEARCH(A2,$B$2:$B$2,1)),"No","Yes")
If case matters:
=IF(ISERROR(FIND(A2,$B$2:$B$2,1)),"No","Yes")

Related

Extracting the different length strings from the middle of a string

I am trying to extract the DocType (VARG, NOR, RMRN, CHNG, ADCN) from the middle of the below worksheet. As you can see, there is nothing consistent within the different strings. I am trying to extract the doctype embedded within the string and place it in the corresponding doctype cell to the left of the drawing sheet cell. Alas, I cannot determine a formula to do so. Any help would be greatly appreciated!
DocType Drawing Sheet
100188-NOR03046
10190635-VARG003-V-013R1
10190635-VARG003-V-018
1086-CHNG121701
10908077-RMRNR0190
11613002-NOR1-1
11627748-NOR07146
11639519-ADCN30352
116-NOR6458
11664680-NOR75941R1
12292527-NORGEO-5343
12292400-NORWIP09335
12292527-NORGEO-5343
I have used this formula:
=MID(I679,SEARCH("-",I679)+1,IF(ISERROR(VALUE(MID(I679,SEARCH("-",I679)+4,1))),4,3))
but for the values listed below I got the following results:
DocType Drawing Sheet Correct Result Should Be
NORG 12292527-NORGEO-5343 NOR
NORW 12292400-NORWIP09335 NOR
VARG 10190635-VARG003-V-013R1 VAR
VARG 12292-VARG003-V-016 VAR
R-AD 12295729-R-ADCN167238 ADCN
31-A 12359705-31-ADCN71449 ADCN
R2-A RM12293172-R2-ADCN183214 ADCN
129 RM-12976612-RM2-ADCN183868 ADCN
19- B5-19-1676-NORFSV00098R1 NOR
NORW 12517164-NORWIP10095 NOR
If you need anymore examples just let me know.
I also tried the following formula but it just produced the 0 (zero):
=IF(I9="*VAR*","VAR",IF(I9="*ADCN*","ADCN",IF(I9="*CHNG*","CHNG",IF(I9="*DEVN*",‌​"DEVN",IF(I9="*EER*","EER",IF(I9="*NOR*","NOR",IF(I9="*PPEP*","PPEP",IF(I9="*RMRN‌*​","RMRN",IF(I9="*SCN*","SCN",IF(I9="*WAIV*","WAIV",0))))))))))
A solution for the case where the required substring is preceded by the first hyphen in the string and followed by the next hyphen or digit:-
=LEFT(RIGHT(B2,LEN(B2)-FIND("-",B2)),
MIN(IF(ISNUMBER(FIND({0,1,2,3,4,5,6,7,8,9,"-"},RIGHT(B2,LEN(B2)-FIND("-",B2)))),
FIND({0,1,2,3,4,5,6,7,8,9,"-"},RIGHT(B2,LEN(B2)-FIND("-",B2)))))-1)
If you have a list of the possible document types you can search for them like this:-
=IFERROR(
INDEX({"ADCN","CHNG","DEVN","EER","NOR","PPEP","RMRN","SCN","VAR","WAIV"},
MATCH("ZZZ",IF(ISNUMBER(FIND( {"ADCN","CHNG","DEVN","EER","NOR","PPEP","RMRN","SCN","VAR","WAIV"},B2)),
{"ADCN","CHNG","DEVN","EER","NOR","PPEP","RMRN","SCN","VAR","WAIV"}))),
"")
"...there is nothing consistent within the different strings" - incorrect. From what I can see, the first 3-4 characters after the first "-" contain the data type. 3 Characters if the 4th character is a number. Use whatever data uniformity exists to create your tests that align your data as needed.
This could work like so [assuming your data starts in B2, this formula goes in C2, and gets dragged down]:
=SEARCH("-",B2)
This gives you the character placement of the first "-" in the cell. Then put this in D2 and drag down:
=ISERROR(VALUE(MID(B2,C2+4,1)))
This attempts to convert the character 4 spaces after the "-" into a value. If it's a letter, it will create an error, which results in TRUE. Otherwise it will show FALSE.
Then put this in E2 and drag down:
=MID(B2,C2+1,IF(D2,4,3))
This says - take cell B2, and, starting at the character after the "-", return the text that goes for 3-4 characters. If D2 is TRUE [there was an error in the above formula, meaning the 4th character was not a number], it goes for 4 spaces. Otherwise if D2 is FALSE, it goes for 3 spaces.
This can alternatively all be placed in a single formula in C2 as follows:
=MID(B2,SEARCH("-",B2)+1,IF(ISERROR(VALUE(MID(B2,SEARCH("-",B2)+4,1))),4,3))

Count the frequency of a specific word in a single cell

In Microsoft Excel I wish to count the frequency of a specific word in a cell. The cell contains a few sentences. I am using a formula right now that is working, but not the way I want it to.
A1
my uncle ate potatos. potato was his favorite food. Don't mash the potato, just keep it simple.
B1 (word to count the frequency of)
potato
C1 (forumula)
=(LEN(A2)-LEN(SUBSTITUTE(A2;B2;"")))/LEN(B2)
C1 Results:
3
In C1, I am getting a count 3. I want it just to be 2. So, the formula is counting potatos.
How do I make the function only count exact matches?
I've got a solution here but it's not pretty.
The problem, as I indicate in my comment, is that Excel has no internal function to see if a cell contains an 'exact match'. You can check if the total value in a cell is an exact match, but you can't check whether a search term has been conjugated like that. So, we'll need to create a special method which checks for every 'acceptable' ending to a word. In my eyes, this would be anything that ends with space, anything that ends with punctuation, and anything at the end of a cell with nothing after it.
ARRAY FORMULAS
You were on the right track with the LEN - SUBSTITUTE method, but the formula will need to be an array formula to work. Array formulas calculate the same thing multiple times over a given range of cells, instead of just once. They resolve the calculation for each individual cell in a formula and provide an array of results. This array of results must be collapsed together to get a single total result.
Consider as follows:
=LEN(C1:C6)
Confirm this formula with CTRL + SHIFT + ENTER instead of just ENTER. This gives us the LEN of C1, followed by C2, C3... etc., resulting in an array of results that looks like this [assume C1 had "a", C2 had "aa", C3 had "a", C4 had "", C5 had "aaa", and C6 had ""]:
={1;2;1;0;3;0}
To get that as a single number providing the total length of each cell individually, wrap that in a SUM function:
=SUM(LEN(C1:C6))
Confirmed again with CTRL + SHIFT + ENTER instead of just ENTER. This results in the total length of all cells: 7.
DEFINING AN EXACT MATCH
Now to take your question, you are looking to find all 'acceptable' matches of given word B1, within text A1. As I said before, we can define an acceptable answer as one which ends in punctuation, a space, or the end of the cell. Something at the end of the cell is a special case which we will consider later. First, take a look at the formula below. In cells C1:C6, I have manually typed a comma, a period, a semi-colon; a hyphen, a space, and a slash. These will be the 'acceptable' ways to end the word found in B1.
=LEN(SUBSTITUTE(A1,B1&C1:C6,""))
Confirmed with CTRL + SHIFT + ENTER, this takes the length of the substitution for the search term in B1 appended with the acceptable word-end in C1:C6. So it gives the length for 6 new SUBSTITUTED words. But as this is an array of results, we need to add them together to get a single number, like so:
=SUM(LEN(SUBSTITUTE(A1,B1&C1:C6,"")))
FORMULIZING THE RESULT
To work it as you have in your sentence, we will now need to subtract this length from the length of the original word. Note that there is a problem with doing this simply - since we are searching multiple times, we will need to add the length of the original word multiple times. Consider something like this:
=LEN(A1)-SUM(LEN(SUBSTITUTE(A1,B1&C1:C6,"")))
This won't work, because it only adds the length of A1 once, but it subtracts the length of the substituted strings multiple times. How about this?
=LEN(A1)*6-SUM(LEN(SUBSTITUTE(A1,B1&C1:C6,"")))
This works, because there are 6 word-end terms we search for with C1:C6, so the substitution there will occur 6 times. So we have the original length of the word 6 times, and the length of each substituted word 6 times [keep in mind that if there is no match for, say, "potato;", then that term will give the length of the original word, thus negating one of the times we added the length of that word, as expected].
To finalize this, we need to divide by the number of letters in the search term. Keep in mind that where you have "/LEN(B1)", we will need to add a character for the length of each of our word-ends.
=(LEN(A1)*6-SUM(LEN(SUBSTITUTE(A1,B1&C1:C6,""))))/(LEN(B1)+1)
Finally, we need to add the special case where the last portion of A1 is equal to the search term, with no word-end. Alone, this would be:
=IF(RIGHT(A1,LEN(B1))=B1,1,0)
This will give us a 1 if the last part of A1 is equal to B1, otherwise it gives 0. So now simply add this to our previous formula, as follows:
=(LEN(A1)*6-SUM(LEN(SUBSTITUTE(A1,B1&C1:C6,""))))/(LEN(B1)+1)+IF(RIGHT(A1,LEN(B1))=B1,1,0)
Remember to confirm with CTRL + SHIFT + ENTER, instead of just ENTER. That's it, it now gives you the count of all "exact matches" of your search term.
ALTERNATE APPROACH TO ARRAY FORMULAS
Note that instead of using C1:C6, you could instead hardcode your formula to look for specific punctuation as the word-end. This will be harder to maintain but, in my opinion, just as readable. It will look like this:
=(LEN(A1)*6-SUM(LEN(SUBSTITUTE(A1,B1&{",",".",";"," ","/","-"},""))))/(LEN(B1)+1)+IF(RIGHT(A1,LEN(B1))=B1,1,0)
This is still technically an "array formula", and it works on the same principles as I have described above. However, one benefit here is that you can confirm this type of entry with just ENTER. This is good, in case someone accidentally edits your cell and presses ENTER without noticing. Otherwise, this is equivilent to the format above.
Let me know if you would like any portion of this elaborated on.
I do have an alternate solution for you to consider. I takes a bit more space and the formulas are a little more convoluted, but in some senses it will be simpler.
Use column C as a new helper column. Column C will take the text from column A, and will substitute out all instances of punctuation with a " ". Once this has been done, the formula to count the instances of the search term from column B will be a simple formula essentially as you have it in your OP.
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,","," "),"."," "),";"," "),"-"," "),"/"," ")
This formula first substitutes all slashes for spaces, then with that substituted text it substitutes dashes for spaces, then with that substited text it substitutes semicolons with spaces, etc. As you indicated, if you use semi-colons as delimiters, you will need to replace my commas separating terms with semi-colons.
Then the formula in D1 is simply what you have above in your OP, with two changes: we will be searching for B1 & " ", because we know all of the 'exact matches' now end in spaces, and we will be adding in an extra '1' if the last part of the text in C1 is the same as the search term in B1 - because if a cell ends in that word, it won't have a space, but it is still an 'exact match'. Like so:
=(LEN(C1)-LEN(SUBSTITUTE(C1,B1&" ","")))/(LEN(B1)+1)+IF(RIGHT(C1,LEN(B1))=B1,1,0)
EDIT
My list of punctuation was only a suggestion; I recommend you really go through some sample text and make sure you don't have any weird characters after words. Also, consider changing uncommon ones I have (like "/", or "-") with "?" or "!". If you want to add more, just follow the pattern of the SUBSTITUTE formula.
To make this case-insensitive, you just need to change the formula in column C to make the result all lower case, and then ensure your search terms in column B are lower case. Change column C like so:
=LOWER(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,","," "),"."," "),";"," "),"-"," "),"/"," "))
Sorry for making it "a new answer". You may move it wherever you want.
I have just found a solution for the answer Liu Kang asked on Aug 3 2015 at 12:15. :)
Unfortunately, I do not have "50 reputation" to comment on Grade 'Eh' Bacon's solution above, where the last comment is this:
Discovered a slight problem. Using =IF(B1<>"";(LEN(A1)-LEN(SUBSTITUTE(A1;B1&" ";"")))/(LEN(B1)+1)+IF(RIGHT(A1;LEN(B1))=B1;1;0);"") with shoe in B1 gives the following result: shoe in A1 = 1 (correct), shoes in A1 = 0 (correct), ladyshoe in A1 = 1 (wrong). Guess this have to do with "RIGHT" in the formula. Is it possible to make the formula non-matching for prefix words? E.g if B1 is containing shoe and A1 is containing ladyshoe dogshoe catshoes shoes I want C1 to result in 0. – Liu Kang Aug 3 '15 at 12:15
The solution is to search for a space at the beginning of the word as well (" "&B1&" ") and to add "one" more LEN(B1)+2. So, it becomes =IF(B1<>"";(LEN(A1)-LEN(SUBSTITUTE(A1;" "&B1&" ";"")))/(LEN(B1)+2)+IF(RIGHT(A1;LEN(B1))=B1;1;0);"").
There is one more problem if the word we are looking for is at the beginning. Because there is obviously no space " " at the beginning of the sentence. I use a workaround for it - I have my sentence in A1, but then I have a hidden column B where there is =" "&A1 in B1 and it puts the "space" I need to the beginning of the sentence and everything from the original Grade 'Eh' Bacon's solution is shifted (A1->B1, B1->C1, C1->D1).
I hope it can help and thanks to all who participated in this thread, you helped me A LOT!
Do you need this to be a single formula? I have an idea, but it takes a few (relaitvely simple) steps.
Since you have a long sentence in A1, what about going to Data -> Text to Columns, and send this sentence into a Row, delimited by spaces. Then, remove any punctuation. Then, just do a simple Countif()?
Put the info in A1, then go to Data --> Text to Columns, choose "Delimited", click Next, and choose "Space":
Click Finish, and it'll put the entire thing into Row 1, with a word in each cell. Now just Find/Replace "." and "," with nothing.
Then, Countif to the rescue!
If that works, we can automate into VB, so you don't have to manually find/replace the puncutation. Before I jump into that, does this method work?
Take the length of the string and minus the length of the string with the keyword replaced with nothing then divide the result by the length of the keyword:
=(LEN(A1)-LEN(SUBSTITUTE(A1,B1,"")))/LEN(B1)

MS Excel: Search, Find and Extract Specific Text Cell containing text string

Although, I've seen relatively similar or close postings
Using MS Excel MS Excel 2010, I would like to be able to search Cell Range (Column A1:A25), to find if specific text within Cell String (Column C2) is a match in Cell Range (A1:A26) and then Output the corresponding matching Keyword results found in same or adjacent cell. If matching text is not found then display "No Match Found"
Although the sample formula shown below that I'm using does work, but only indicates it found or did not find word within Cell Range I need for it to return the actual matching Keyword text found versus "FOUND" OR "NOT FOUND".
=IF(SUM(IFERROR(FIND(A1:A26,C2),0))>0, "FOUND", "NOT FOUND")
Example:
Cell String contains the following text in Cell C2:
"I found a lost German Sheppard in my backyard on yesterday"
Keyword Search Words: Column A1:A26
TYPES OF DOGS
Affenpinscher
Afghan Hound
Airedale Terrier
Akita
Alaskan Malamute
American Foxhound
American Staffordshire Terrier
American Water Spaniel
Anatolian Shepherd
French Bulldog
German Pinscher
German Shepherd
German Shorthaired Pointer
German Wirehaired Pointer
Giant Schnauzer
Glen of Imaal Terrier
Golden Retriever
Gordon Setter
Great Dane
Greater Swiss Mountain
Great Pyrenees
Greyhound
Harrier
Irish Setter
Irish Terrier
The Returned and displayed results/answer = German Sheppard
Please let me know if this possible, as I would greatly appreciate any assistance to resolving my question.
Miaka3
Assuming you are searching column A for a string(s) in column C type this array formula in B:
=IFERROR(INDEX($C$2:$C$6,MAX(IF(ISERROR(FIND($C$2:$C$6,A2)),-1,1)*(ROW($C$2:$C$6)-ROW($C$2)+1))), "None")
You can see here that I have a search list of multiple characters/strings in C2 through C6. If one of the search strings is found, it will be listed in column B, otherwise it will display "None".
The key to this working is copy the above formula into cel B2 and while you are still in edit mode pressing Ctrl+Shift+Enter instead of just Enter like usual. You will know it works correctly if you see the addition of { and } in the formula once you hit the three key combination. After that you can copy the formula down through A25 or wherever you need.
Note you can make your Search List longer or shorter as needed, just make sure to update your formula to reflect the range. You will want to fill in all of you dog breeds in Column C and change every $C$2:$C$6 in the formula to equal your search list range. And, remember, any change to the array formula requires Ctrl+Shift+Enter to work properly.
This isn't what Excel was designed for, but you can accomplish it with a helper column. Insert a new column at B (this will move C2 to D2). Then paste this formula into B2:
=IF(ISERROR(FIND(A2,D$2)),"",A2)
Fill down that formula until A27 (you said A26, but your example went to A27 if you include the header "TYPE OF DOGS"). This will output the breed of the dog only if it is contained within the text in D2.
Finally, go to whatever cell you would like to contain your output, and paste in:
=CONCATENATE(B2,B3,B4,B5,B6,B7,B8,B9,B10,B11,B12,B13,B14,B15,B16,B17,B18,B19,B20,B21,B22,B23,B24,B25,B26,B27)

Search a string for certain amount of spaces

I am looking for a formula which allows me to search a certain number of " " starting from the end of the text (RIGHT) in a text string in cell A, and then it looks what is on the RIGHT of the text string and search for that bit of text for exact match against a text range in another column text column B.
If exact match is found it should pick what is in corresponding column C and return that value. If more then one match is found in column B then an ERROR should be given.
I have following bit of code which looks at the 3rd space but doesn't return all of the text on the RIGHT hand side
=RIGHT(B8,SEARCH(" ",B8,SEARCH(" ",B8,SEARCH(" ",B8)+1)+1)-1)
and I still require the compare text formula to cell B and C.
Try:
=TRIM(RIGHT(SUBSTITUTE(B8," ",REPT(" ",777)),2331))
though I'm not at all clear about the other parts to your question. Perhaps you could give some examples?
Regards
Maybe try:
=IF(COUNTIF(B:B,RIGHT(A8,LEN(A8)-FIND(" ",A8,FIND(" ",A8,FIND(" ",A8)+1)+1)))>1,"ERROR",VLOOKUP(RIGHT(A8,LEN(A8)-FIND(" ",A8,FIND(" ",A8,FIND(" ",A8)+1)+1)),B:C,2,0))
and adjust the cell references if required.

How can I switch a string from "lastName, firstName" to "firstName LastName"?

I have a column full of names written as:
"lastName, firstName"
I want to have a nother column that has this name list written as:
"firstName LastName"
So, how can I switch a string from "lastName, firstName" to "firstName LastName" ?
If the first name is in A2 try this formula in B2 copied down
=MID(A2&" "&A2,FIND(" ",A2)+1,LEN(A2)-1)
Enter data into cells e.g.
Brown, John
Green, Bob
Smith, Will
(Note that the comma in this case is the delimiter which the system will use to separate the entries)
Highlight these cells.
Click on the "Data" tab,
click on "Text to Column".
Choose options offered.
The worked for me
=RIGHT(C3,LEN(C3)-LEN(LEFT(C3,FIND(",",C3)-1))-1) & " " & LEFT(C3,FIND(",",C3)-1)
Barry's answer is correct, if the single cell (A2) doesn't ALSO contain errors like "firstname lastname" -- as is often the case.
So, to deal with the cell data being either "Last, First" or "First Last", we'll need to compare the cell and take action appropriately. Also note, in my example, I moved the first name to it's own column, and last name to it's own column. You can of course join them into a single column.
First, let's break down what we need. This will return the first name when there is a "," in the cell contents: (assumes the field you are wanting to split is in A2, adjust as needed)
=MID(A2;FIND(",";A2)+2;99)
and this will return the first name when there isn't:
=MID(A2;1;FIND(" ";A2))
And for Last name when there is a comma:
=MID(A2;1;FIND(",";A2)-1)
and last name when there isn't:
=MID(A2;FIND(" ";A2)+1;99)
So, we need to test if there is a "," in the string, and deal with an error condition when there isn't (FIND returns #value when not found, ISERROR() can test for that). Optionally (and my approach) was to check if the return of the FIND yielded a number (an error won't). In this case, an "error condition of not finding it" would yield a non-number #Value.
=IF( ISNUMBER( FIND(",";A2)); MID(A2;FIND(",";A2)+2;99); MID(A2;1;FIND(" ";A2)))
Doing the same thing for the Last name looks like this:
=IF( ISNUMBER( FIND(","; A2)); MID(A2;1;FIND(",";A2)-1); MID(A2;FIND(" ";A2)+1;99))
Please note that my solution was using my spreadsheet application OpenOffice. I think Excel has a different find method (Search?) - so make adjustments as necessary.
The above answer is incorrect.
In fact, for my own name, Jay Jacob Wind, the formula breaks.
The correct formula is:
Assuming Last, First is in column A, separated by one comma and a space bar
For first name:
=mid(A2,find(",",A2)+2,99)
For last name:
=mid(A2,1,find(",",A2)-1)
Put together, assuming Column B is First Name and Column C is Last Name
=B2&" "&C2
or simply (but more complexly)
=mid(A2,find(",",A2)+2,99)&" "&mid(A2,1,find(",",A2)-1)
but I like separating them into two columns,
then concatenating them

Resources