Author: Oscar Cronquist Article last updated on December 17, 2018

The image above demonstrates a formula in cell F3 that extracts unique distinct values from column B if they contain the value in cell D3.

Formula in cell F3:

=LOOKUP(2, 1/((COUNTIF(\$F\$2:F2, \$B\$3:\$B\$21)=0)*SEARCH(\$D\$3, \$B\$3:\$B\$21)), \$B\$3:\$B\$21)

### Explaining formula in cell F3

#### Step 1 - Prevent duplicates

The COUNTIF function counts cells in cell range based on a condition or criteria. If the value is equal to 0 then it has not been displayed yet.

COUNTIF(\$F\$2:F2, \$B\$3:\$B\$21)=0

becomes

COUNTIF("Search results "contain" search value
Unique distinct list", {"Federer, Roger "; "Djokovic, Novak "; "Murray, Andy "; "Davydenko, Nikolay "; "Roddick, Andy "; "Del Potro, Juan Martin "; "Federer, Roger "; "Davydenko, Nikolay "; "Verdasco, Fernando "; "Gonzalez, Fernando "; "Wawrinka, Stanislas "; "Gonzalez, Fernando "; "Blake, James "; "Nalbandian, David "; "Robredo, Tommy "; "Wawrinka, Stanislas "; "Cilic, Marin "; "Stepanek, Radek "; "Almagro, Nicolas "})=0

becomes

{0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0}=0

and returns

{TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE}

#### Step 2 - Check if values contain string

The SEARCH function returns a number that represents the position of the search string if found. The function returns an error if not found which is alright in this case.

SEARCH(\$D\$3,\$B\$3:\$B\$21)

becomes

SEARCH("r",\$B\$3:\$B\$21)

and returns

{5; #VALUE!; 3; #VALUE!; 1; 8; 5; #VALUE!; 3; 13; 4; 13; #VALUE!; #VALUE!; 1; 4; 10; 11; 6}.

#### Step 3 - Multiply arrays

Both values must be TRUE in order to be TRUE meaning if the value has not been displayed yet AND the value contains the string then return TRUE or the equivalent numerical number. TRUE is all numbers except 0 (zero), FALSE is 0 (zero).

{TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE; TRUE}* {5; #VALUE!; 3; #VALUE!; 1; 8; 5; #VALUE!; 3; 13; 4; 13; #VALUE!; #VALUE!; 1; 4; 10; 11; 6}

and returns

{5; #VALUE!; 3; #VALUE!; 1; 8; 5; #VALUE!; 3; 13; 4; 13; #VALUE!; #VALUE!; 1; 4; 10; 11; 6}.

#### Step 4 - Divide 1 with array

The result will return !DIV/0 error if 1 is divided with 0 (0), which the LOOKUP function ignores. It will also ignore #VALUE! errors.

1/((COUNTIF(\$F\$2:F2, \$B\$3:\$B\$21)=0)*SEARCH(\$D\$3, \$B\$3:\$B\$21))

becomes

1/{5; #VALUE!; 3; #VALUE!; 1; 8; 5; #VALUE!; 3; 13; 4; 13; #VALUE!; #VALUE!; 1; 4; 10; 11; 6}

and returns

{0.2; #VALUE!; 0.333333333333333; #VALUE!; 1; 0.125; 0.2; #VALUE!; 0.333333333333333; 0.0769230769230769; 0.25; 0.0769230769230769; #VALUE!; #VALUE!; 1; 0.25; 0.1; 0.0909090909090909; 0.166666666666667}.

#### Step 5 - Return value

LOOKUP(2,1/((COUNTIF(\$F\$2:F2,\$B\$3:\$B\$21)=0)*SEARCH(\$D\$3,\$B\$3:\$B\$21)),\$B\$3:\$B\$21)

becomes

LOOKUP(2,{0.2; #VALUE!; 0.333333333333333; #VALUE!; 1; 0.125; 0.2; #VALUE!; 0.333333333333333; 0.0769230769230769; 0.25; 0.0769230769230769; #VALUE!; #VALUE!; 1; 0.25; 0.1; 0.0909090909090909; 0.166666666666667},\$B\$3:\$B\$21)

becomes

LOOKUP(2,{0.2; #VALUE!; 0.333333333333333; #VALUE!; 1; 0.125; 0.2; #VALUE!; 0.333333333333333; 0.0769230769230769; 0.25; 0.0769230769230769; #VALUE!; #VALUE!; 1; 0.25; 0.1; 0.0909090909090909; 0.166666666666667}, {"Federer, Roger "; "Djokovic, Novak "; "Murray, Andy "; "Davydenko, Nikolay "; "Roddick, Andy "; "Del Potro, Juan Martin "; "Federer, Roger "; "Davydenko, Nikolay "; "Verdasco, Fernando "; "Gonzalez, Fernando "; "Wawrinka, Stanislas "; "Gonzalez, Fernando "; "Blake, James "; "Nalbandian, David "; "Robredo, Tommy "; "Wawrinka, Stanislas "; "Cilic, Marin "; "Stepanek, Radek "; "Almagro, Nicolas "})

and returns

"Almagro, Nicolas " in cell F3.