Excel formula to check if items from a comma separated list in one cell exist in a comma separated list in another cell? - excel

I have a spreadsheet where every row has two columns, each containing a comma separated list of words or phrases.
Column 1 | Column 2
---------------------------------------------------------
Orange, Pear, Sugar apple, Kiwi | Orange, Sugar apple
Banana, Watermelon, Pomegranate | Strawberry, Banana
I'm trying to create a formula that checks if the items listed in Column 2 are a subset of the items listed in Column 1 and outputs true or false.
In the above example the output should be true for Row 1 and false for Row 2.
The solutions I tried using the search and find functions only work if the items in Column 2 are listed in the same sequence, i.e. if Column 2 is a substring of Column 1.

Use this array formula:
=AND(ISNUMBER(SEARCH(", " & TRIM(MID(SUBSTITUTE(B1,",",REPT(" ",99)),(ROW($XFD$1:INDEX(XFD:XFD,LEN(B1)-LEN(SUBSTITUTE(B1,",",""))+1))-1)*99+1,99)) & ",",", "&A1&",")))
Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode

Related

Excel Formula to return all rows and columns corresponding to search string

I'm trying to lookup two sheets in Spreadsheet.
One has the search value and other has all the data corresponding to the search string.
I'm trying to find a formula that will search the value and return all the corresponding rows. Another thing is that it has to return the entire row and columns correspoing to that string and not only one.
I'm using this Vlookup formula as of now but any help will do:=
ArrayFormula(VLOOKUP(A1,Sheet2!A2:AE196,{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24},FALSE))
A2:is the seach string, and used array to define the Columns to be returned.
But the formula returns only one row corresponding to the string and not all the rows.
Is there a formula that returns all rows and entire values in those rows correspoinding to the search string?
EX :Sheet1:search criteria
Search
Apple
Mango
melon
Ex:Sheet 2: Data
Name
Colour
no
Apple
red
5
Banana
yellow
3
Apple
red
25
Mango
yellow
1
Mango
yellow
10
Expected result for Apple(A1 in the formula) as search string:
Name
Colour
no
Apple
red
5
Apple
red
25
Actual result:
Name
Colour
no
Apple
red
5
You should use FILTER or QUERY
=FILTER(Sheet2!A2:AE196,Sheet2!A2:A196=A1)
=QUERY(Sheet2!A2:AE196,"select * where A='"&A1&"'",1)

Excel Substitute a substring in cell (sheet A) by the corresponding string in "lookup" (sheet B)

How to do this in MS Excel 15.4
I want to process Column A to become Column B
Column A | Column B
----------------------------------------------------------------------
one, 1, two, 2, three, 3 | one apple, two bananas, three strawberries
one, 1, four, 4 | one apple, four oranges
.......................
... many other rows ...
.......................
two, 2, four, 4, three, 3 | two bananas, four oranges, three strawberries
The Column A can have n matching substrings in the lookup sheet.
I have another sheet (lookup table) with what to substitute the text in Column A with
Match col | Replace col
----------------------------
one, 1 | one apple
two, 2 | two bananas
three, 3 | three strawberries
four, 4 | four oranges
... and many more ...
I want to replace all the substrings found in Column A with the Replace col value of the lookup table
It looks like I may be able to combine VLOOKUP with SUBSTITUTE, but I am struggling with it
To do it in indivdual cells;
=IFERROR(VLOOKUP(TRIM(SUBSTITUTE(MID(SUBSTITUTE($A1,",",REPT(" ",999)),(COLUMN(A:A)-1)*2*999+1,2*999),REPT(" ",999),",")),Sheet2!$A:$B,2,FALSE),"")
If you have a subscription to Office 3651 excel you can use this array formula to put it all in one cell:
=TEXTJOIN(",",TRUE,IFERROR(LOOKUP(TRIM(SUBSTITUTE(MID(SUBSTITUTE($A1,",",REPT(" ",999)),(ROW(INDIRECT("1:" & INT((LEN($A1)-LEN(SUBSTITUTE($A1,",","")))/2)+1))-1)*2*999+1,2*999),REPT(" ",999),",")),Sheet2!A:A,Sheet2!B:B),""))
Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.
On caveat is that the reference data must be sorted on the lookup column:
1 If you do not have Office 365 but want to use this formula you can place the code from my answer HERE that will mimic the TEXTJOIN() in a module attached to the worksheet. Then use the formula as described above
I have a rather clunky solution, but it'll work for you if you don't mind taking perhaps a few extra steps. (No VBA required).
With your original data, highlight all of it and do Text to Columns with a comma delimiter. Set the destination to wherever you like. I chose the column just right of it (so, B2):
So now you have it all split up.
I put the VLOOKUP() table in "Sheet2":
And back on Sheet1, in I2, I used this formula:
=IFERROR(VLOOKUP(TRIM(B2)&", "&TRIM(C2),Sheet2!$A$1:$B$4,2,FALSE),"")
And drag right. You'll have some empty columns which you can hide/Delete, then copy all the data.

Count how many distinct values (or get list of distinct values) in a filtered column

Is there a way to count the number of distinct values in a filtered column in Excel?
Using the formula at https://exceljet.net/formula/count-unique-values-in-a-range-with-countif I can count the number of distinct values in a range, but when that range has been filtered with an auto-filter I still get the unfiltered count.
For example:
A B
1 Scarf Blue
2 Hat Red
3 Gloves Green
4 Coat Blue
5 Balloon Red
6 Shoes Blue
Counting unique values in B with =SUMPRODUCT((B1:B6<>"") / COUNTIF(B1:B6,B1:B6 & "")) should return 3 as the distinct values are Red, Green and Blue.
If I auto filter Column B to just select Red items, the resulting table will look like:
A B
2 Hat Red
5 Balloon Red
In this case the number of distinct values retuned should be 1. But the formula above still returns 3.
The formula should also cope with multiple selections in the auto-filter, so for example filtering for Blue and Green should result in the following table:
A B
1 Scarf Blue
3 Gloves Green
4 Coat Blue
6 Shoes Blue
From which the formula should return 2 (Blue, Green).
Finally, if I am filtering on column A rather than B, the formula should still work. So If I am only interested in Hat, Scarf and Coat, filtering column A for these values would result in:
A B
1 Scarf Blue
2 Hat Red
4 Coat Blue
From which the formula should return 2.
(I'm using Excel 2013 and need to do this in a formula rather than using VBA etc)
I also found this page on office.com which I thought might help, but alas I can't get it to work for me.
This reference shows how you can exclude hidden rows using AGGREGATE
Excluding hidden rows with AGGREGATE
You can then use a standard way of counting unique values like this
Counting unique values with FREQUENCY
So if you were counting values in column B, you would need a helper column (say C) containing
=IF(AGGREGATE(3,5,B2),B2,"")
Then plug in the form of count unique that ignores empty cells
=SUM(IF(FREQUENCY(IF(LEN(C2:C10)>0,MATCH(C2:C10,C2:C10,0),""), IF(LEN(C2:C10)>0,MATCH(C2:C10,C2:C10,0),""))>0,1))
Or your formula if you prefer
=SUMPRODUCT((C2:C10<>"") / COUNTIF(C2:C10,C2:C10 & ""))

How can I check against a cell and it's opposite cell?

I'm trying to check if my cells contain a specific text, and if so, if the cell opposite my 'specific text' contains another specific text.
A| B
1, Banana
2, Orange
3, Banana
1, Banana
I want to count the number of occurrences a specific text has the word banana in the cell opposite.
My thought process is something along the lines of =IF(A:A=1,=COUNTIF(B:B,"Banana"),"")
I'm constantly adding in new cells aswell.
I only want the count the number of times a specific text has banana next to it.
How can I do this?
Use COUNTIFS() formula for matching multiple criteria in multiple ranges.
=COUNTIFS(A1:A4,1,B1:B4,"Banana")
This will count the number of time a cell in column A has 1 and Column B in the same row has Banana
=SUMPRODUCT((A:A=1)*(B:B="Banana"))
if you are checking for a string instead of "1", make sure to put it in quotes

How to Count the number of times a cell in one column matches a cell in another column?

I have two columns. Column A is a list of text values. Column B will be individual text values that may or may not match Column A. Across say 20 rows, I want to use something like COUNTIF at the bottom of Column B to count how many answers in Column B match the correct answer in Column A.
For example, if Column A reads:
1. Apple
2. Orange
3. Banana
4. Kumquat
5. Pineapple
Column B reads:
1. Apple
2. Guava
3. Pistachio
4. Kumquat
5. Pineapple
Essentially, rows 1, 4 & 5 all match, thus the sum at the bottom of column B would be 3.
Is there a way to do this with a formula?
You can use something like this:
{=SUM(IF(A1:A5=B1:B5,1,0))}
To use this formula, type in any cell "=SUM(IF(A1:A5=B1:B5;1;0))" and press CTRL+SHIFT+INTRO

Resources