Home / Guides

How to compare two lists in Excel and Google Sheets, and the traps in each method

Every formula that compares lists in a spreadsheet has a documented quirk: wildcards, a 255-character limit, capitals, spaces you cannot see. Here is what Microsoft and Google say about each method, and which one to use.

The quick answer: compare two columns in Excel

With list A in column A and list B in column B, put this in C2 and fill it down:

=IF(COUNTIF($B:$B, A2)>0, "In both", "Only in A")

It works for most lists. It gives wrong answers in four situations that Microsoft documents: items containing *, ? or ~, items longer than 255 characters, numbers stored as text in one list and as numbers in the other, and items with a no-break space. The rest of this guide shows each one and the fix.

The methods side by side

MethodWhere it worksCapitalsTreats * ? ~ as wildcardsDocumented trap
COUNTIFEvery Excel version, Google SheetsIgnoredYesWrong results for text over 255 characters (Excel)
MATCH(x, range, 0)Every Excel version, Google SheetsIgnoredYes, in exact match modeSame wildcard issue as COUNTIF
VLOOKUP(x, range, 1, FALSE)Every Excel version, Google SheetsNot stated on Microsoft's pageYes, for text#N/A when a number is stored as text
XLOOKUPMicrosoft 365, Excel 2021 and 2024 (not 2016 or 2019)Only if you ask for it (match mode 2)Not available in older versions
EXACTEvery versionCounted (case-sensitive)NoCompares two cells, not a cell and a list
UNIQUEMicrosoft 365, Excel 2021 and 2024; Google SheetsNoNot in older Excel
Data, Remove DuplicatesExcelNoWorks on displayed values and deletes permanently

The behavior columns describe Excel, from Microsoft's function pages. Empty cells mean the official page does not say, and we did not guess.

Trap 1: wildcards in your own data

COUNTIF, MATCH and VLOOKUP read * and ? in the value you look up as wildcards. Microsoft's COUNTIF page: "A question mark matches any single character. An asterisk matches any sequence of characters." So if list A contains the product code AB*, COUNTIF(B:B, "AB*") counts AB1, AB-200 and every other code starting with AB, and the item is reported as "In both" when it is not.

It matters for SKUs, part numbers, passwords, search queries and regular expressions. Two fixes:

  • Escape the characters: Microsoft says to "type a tilde (~) in front of the character" to find a real ? or *. In a formula: COUNTIF(B:B, SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"~","~~"),"*","~*"),"?","~?")).
  • Use XLOOKUP, where exact match is the default and wildcards apply only in match mode 2, or compare cells with =, which has no wildcards: =SUMPRODUCT(--($B$2:$B$5000=A2))>0.

Trap 2: items longer than 255 characters

Microsoft's COUNTIF page says the function "returns incorrect results when you use it to match strings longer than 255 characters." Long URLs with tracking parameters, file paths, product descriptions and addresses easily pass 255 characters. For lists of URLs, use the = comparison above, or shorten the URLs first: the URL extractor can strip utm tracking parameters.

Trap 3: spaces you cannot see

The usual advice is to wrap both lists in TRIM. It does not remove the most common hidden character. Microsoft: TRIM was "designed to trim the 7-bit ASCII space character (value 32)", and "by itself, the TRIM function does not remove this nonbreaking space character" (character 160, which web pages and many exports use). Microsoft's data-cleaning page recommends SUBSTITUTE for character 160 and other high characters. A formula that handles both:

=TRIM(SUBSTITUTE(A2, CHAR(160), " "))

Google Sheets has the same blind spot: its Data cleanup, Trim whitespace option removes leading, trailing and repeated spaces, but Google notes that a non-breaking space "will not be trimmed." Zero-width spaces are not touched by either. To see what is really in a cell, paste it into the hidden character detector.

Trap 4: numbers stored as text

An ID typed as 1042 in one list and stored as the text '1042 in the other looks identical but does not match. Microsoft lists it as a cause of #N/A: when "you try to have VLOOKUP reference a number, but the source data is stored as text." Convert one side (=VALUE(A2) or =A2&"") so both are the same type. Leading zeros make it worse: Excel turns 00721 into 721 when it opens a CSV file (see opening CSV files without losing data).

Capitals: who ignores them and who does not

  • Excel COUNTIF: "Criteria aren't case sensitive", so apples and APPLES match.
  • Excel MATCH "does not distinguish between uppercase and lowercase letters."
  • Excel EXACT is case-sensitive: EXACT("word","Word") returns FALSE. For a case-sensitive list check, use =SUMPRODUCT(--EXACT($B$2:$B$5000, A2))>0.
  • Google Sheets COUNTIF "is not case sensitive" either.
  • Google Sheets Remove duplicates treats "identical values but different letter cases" as duplicates, so Apple and apple collapse into one row.

Remove Duplicates: two things to know first

  • Excel decides what is a duplicate by "what appears in the cell, not the underlying value stored in the cell". Microsoft's own example: the same date shown as 3/8/2006 in one cell and Mar 8, 2006 in another is not treated as a duplicate. Format both columns the same way first.
  • It keeps "the first occurrence of the value in the list" and Microsoft warns "you are permanently deleting duplicate values." Copy the sheet first, or use UNIQUE, which leaves the original untouched. UNIQUE's exactly_once argument returns only the values that appear once, which Microsoft calls "the database concept of unique."

Size limits

An Excel sheet holds 1,048,576 rows by 16,384 columns. A Google Sheets file holds up to 20 million cells. COUNTIF on two full columns of several hundred thousand rows is slow because every cell is checked against the whole other column; the online list comparer uses a hash table instead and compared two lists of 500,000 items in about half a second in our test.

Which method to use

  • Short lists of simple words: COUNTIF is fine.
  • Codes or text with *, ?, ~, or items over 255 characters: XLOOKUP or the = comparison.
  • Capitals matter: EXACT inside SUMPRODUCT.
  • Data from web pages, PDFs or other systems: clean character 160 first, or compare with a tool that ignores hidden characters.
  • Both directions at once (only in A, only in B, in both) without formulas: paste both columns into Compare two lists.

Tools for this

Sources