简体   繁体   中英

Search Row for Specific Text and Copy it to Cell

I wonder if anyone can help with this:

I have a sheet with rows of data. Within each row there will be a single occurrence of either 'x' or 'y', among many other entries in other columns. The string is the only text in the cell. No row has both 'x' and 'y'. I'd like to identify 'x' or 'y' in each row and copy the value to a new column.

In layman's terms something like;

IF row contains "x" write "x"
  else If row contains "y" write "y"
  else write ""
end

I have managed to get some kind of multi step process going where I use the formula below. But it is super clunky and requires many steps, copy, pasting, results etc and won't fly for the guy on Monday.

=IF(ISNUMBER(SEARCH(substring,text)), "x", "y")

Many thx D

You can do nested IF statements. You didn't specify where you want the formula in relation to the data, but if you wanted the result in column A then you could do something like this (assuming you want the result in cell A1 and looking at data in cells in range B1:IV1)...

=IF(NOT(ISERROR(MATCH("y",$B1:$IV1,0))),"y",IF(NOT(ISERROR(MATCH("x",$B1:$IV1,0))),"x",""))

The basis of this is the MATCH() function. When a match is NOT found in the range, it returns a #NA error, otherwise it returns the location index of where the first match is. Since it throws an error when it doesn't find a match, I've wrapped the MATCH in the ISERROR function to turn it into a TRUE/FALSE boolean where TRUE is returned when it is an error and FALSE returned when it's not an error. Since that TRUE/FALSE is opposite of what you're looking for, I wrapped that in the NOT() function to reverse it...returning TRUE when it finds the match of either 'x' or 'y'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM