John Serra
Back to Blog
AnalyticsBusiness OperationsData AuditData QualityreportingSmall Business AnalyticsSpreadsheet Managementspreadsheets

Remove Trailing Spaces and Hidden Characters That Break XLOOKUP

Five invisible characters can leave $7,500.00 of orders without a region in XLOOKUP. Here is how to find them, clean both order and master keys safely, and prove every order matched.

Business numbers often fall out of sync across systems. If XLOOKUP returns “NOT FOUND” for a code that looks right, clean both the lookup key and the master key with =TRIM(CLEAN(SUBSTITUTE(B2, CHAR(160), " "))). Then check that the cleaned master keys are still unique. Neither TRIM nor CLEAN removes a non-breaking space. In the example below, five dirty codes leave $7,500.00 of orders without a region.

My preferred tool is Google Sheets because a spreadsheet should be shareable and make collaboration easy. Every formula below also works in Excel for the web. The two tools differ in three places, all noted below: what TRIM does to tabs and line breaks, how XLOOKUP treats two invisible characters, and one Find and Replace setting.

The results below come from a test of this exact workbook in Google Sheets and Excel for the web, both in US English. One optional formula in the questions at the end was not part of that test and is marked.

The 8-Order Export and What Each Cleaning Step Recovers

Below is the 8-order export I had generated for this article. It sits on one worksheet named Clean_Keys, with headings in A1:C1 and orders in rows 2 to 9. A 5-row customer master sits in O1:P6. It maps CUST-101 through CUST-105 to the regions North, South, East, West, and Central. Total order sales are $12,000.00.

Five of the eight customer codes carry a character you cannot see. The “What is hidden” column is a reading guide and is not part of the sheet. Do not paste the dirty codes from this table, because a web page cannot reliably carry them. The setup steps in “How to Set Up and Resize the Worksheet” below show how to create them.

Row order_id (A) raw_customer_code (B) order_amount_usd (C) What is hidden
2 ORD-7801 CUST-101 $1,500.00 Nothing
3 ORD-7802 CUST-102 $1,800.00 Trailing space (code 32)
4 ORD-7803 CUST-103 $1,200.00 Leading space (code 32)
5 ORD-7804 CUST-104 $2,200.00 Trailing non-breaking space (code 160)
6 ORD-7805 CUST-105 $1,400.00 Trailing line break (code 10)
7 ORD-7806 CUST-101 $900.00 Trailing tab (code 9)
8 ORD-7807 CUST-102 $1,600.00 Nothing
9 ORD-7808 CUST-103 $1,400.00 Nothing

A plain XLOOKUP finds a region for only the three clean rows. Here is how much each cleaning step recovers. Total order sales stay at $12,000.00 in every row. Cleaning only changes how much of that total gets a region.

Cleaning step Matched orders Sales with a region Sales without a region What still fails
1. Raw XLOOKUP, no cleaning 3 of 8 $4,500.00 $7,500.00 All five hidden characters
2a. TRIM only, Excel for the web 5 of 8 $7,500.00 $4,500.00 Non-breaking space, line break, tab
2b. TRIM only, Google Sheets 7 of 8 $9,800.00 $2,200.00 Non-breaking space
3. TRIM and CLEAN 7 of 8 $9,800.00 $2,200.00 Non-breaking space (ORD-7804)
4. Full clean against a cleaned master 8 of 8 $12,000.00 $0.00 Nothing on this export
Excel for the web view of Clean_Keys!U1:X5 showing four cleaning stages across the eight orders: 1_Raw_XLOOKUP matches 3 rows ($4,500.00, $7,500.00 unmatched), 2_TRIM_Only matches 5 rows ($7,500.00, $4,500.00 unmatched), 3_TRIM_CLEAN_Only matches 7 rows ($9,800.00, $2,200.00 unmatched), and 4_TRIM_CLEAN_SUB160 matches all 8 rows ($12,000.00, $0.00 unmatched).
In Excel for the web, Clean_Keys!U1:X5 tracks matched orders rising from 3 ($4,500.00) raw to 5 ($7,500.00) with TRIM only, 7 ($9,800.00) with TRIM and CLEAN, and 8 ($12,000.00, $0.00 unmatched) after removing CHAR(160) and cleaning the master keys. Open full-size image.

Step 2 is the first place the two tools differ. In Excel for the web, TRIM removed only the regular spaces. In Google Sheets, it also removed the trailing line break and tab. Both tools still missed the $2,200.00 order with the non-breaking space. Only step 4 matched all eight orders in both tools.

If the master table is missing a code altogether, cleaning will not help. That is a different problem, covered in Find Unmatched Customer IDs with XLOOKUP. If the codes differ in spelling, such as “Acme Co.” and “Acme Corp”, see Map Inconsistent Customer Names to Stable IDs.

Step 1: Prove Invisible Characters Are Breaking the Lookup

Do not judge a key by how it looks. Three quick tests show a hidden character in seconds:

  • =LEN(B3) returns 9 for “CUST-102” with a trailing space. The visible code has 8 characters.
  • =EXACT(B3, "CUST-102") returns FALSE. EXACT compares every character, including invisible ones.
  • =CODE(RIGHT(B3, 1)) returns 32, the code for a regular space. A non-breaking space returns 160, a line break 10, and a tab 9.

The worksheet runs these tests on every row in three helper columns. Raw column B is never edited.

Column F (Clean_Key) holds the cleaned code. Step 2 explains the formula.

Column G (Key_Changed_Flag) compares the raw code with the cleaned code using EXACT. It returns 1 when they differ and 0 when they match. Because EXACT checks characters, not length, it also catches a swap that keeps the length the same. For example, “CUST 101” written with a non-breaking space in the middle becomes “CUST 101” with a regular space. The length stays 8, but G still returns 1.

Column H (Edge_Char_Code) shows which character was removed. It reads the first character of the raw code, then the last. If either differs from the cleaned code, H returns that character’s code. If both ends match but the text still changed, H returns "INTERIOR". For example, “CUST-1”, a tab, and “01” cleans to “CUST-101”, and H returns "INTERIOR" because the tab sat in the middle.

Here is what the helper columns return on the export:

Row order_id (A) Clean_Key (F) Key_Changed (G) Edge_Char_Code (H) Raw_XLOOKUP (I) TRIM_XLOOKUP (J) TRIM_CLEAN (K) Clean_XLOOKUP (L) IFERROR_Raw (M)
2 ORD-7801 CUST-101 0 0 North North North North $1,500.00
3 ORD-7802 CUST-102 1 32 NOT FOUND South South South $0.00
4 ORD-7803 CUST-103 1 32 NOT FOUND East East East $0.00
5 ORD-7804 CUST-104 1 160 NOT FOUND NOT FOUND NOT FOUND West $0.00
6 ORD-7805 CUST-105 1 10 NOT FOUND NOT FOUND Central Central $0.00
7 ORD-7806 CUST-101 1 9 NOT FOUND NOT FOUND North North $0.00
8 ORD-7807 CUST-102 0 0 South South South South $1,600.00
9 ORD-7808 CUST-103 0 0 East East East East $1,400.00
Google Sheets view of Clean_Keys!A1:M9 showing orders ORD-7801 through ORD-7808 with rows 3 to 7 highlighted, Key_Changed_Flag and Edge_Char_Code diagnostics (32, 32, 160, 10, 9), Raw_XLOOKUP returning NOT FOUND on rows 3 to 7, Google Sheets TRIM_XLOOKUP matching Central on row 6 and North on row 7 while leaving ORD-7804 NOT FOUND, Clean_XLOOKUP matching all eight orders, and IFERROR_Raw_Amount showing $0.00 on all five dirty rows.
In Google Sheets, Clean_Keys!A1:M9 exposes all five hidden character codes (32, 32, 160, 10, and 9) while keeping column B untouched; unlike the Excel for the web values in the table above, Google Sheets TRIM_XLOOKUP returns Central on row 6 and North on row 7 while still leaving ORD-7804 (CHAR(160)) as NOT FOUND. Open full-size image.

The TRIM_XLOOKUP column shows the Excel for the web results. In Google Sheets, J6 returns Central and J7 returns North, because its TRIM also removed the line break and tab. Column D (Row_Check) and column E (Order_ID_Freq) are not shown. D validates each row, and E counts how often each order ID appears, so a duplicate stops the report.

EXACT exposes a changed key in both tools. XLOOKUP is less consistent. In the test, a code with a non-breaking space in the middle failed to match a master code with a regular space in Excel for the web. Google Sheets matched them anyway. So a lookup that works in one tool can fail in the other. Trust G and H before you trust a match.

Step 2: Clean Both Order Keys and Master Keys in the Right Order

Each function removes a different set of characters:

  • TRIM removes regular spaces (code 32) from both ends and collapses repeated spaces inside the text. Both vendors document that it leaves non-breaking spaces (code 160) in place (Microsoft Excel TRIM, Google Sheets TRIM). As the table above shows, Google Sheets TRIM also removed a trailing tab and line break in the test, while Excel for the web left them.
  • CLEAN removes the nonprinting characters with codes 0 to 31, which include tabs (9), line feeds (10), and carriage returns (13). It does not remove code 160 or other Unicode characters (Microsoft Excel CLEAN, Google Sheets CLEAN).
  • SUBSTITUTE(B2, CHAR(160), " ") swaps each non-breaking space for a regular space, which TRIM can then remove.

So the cleaning formula in F2 works from the inside out: swap non-breaking spaces, remove control characters, then trim spaces. The worksheet version adds a row check in front:

=IF(NOT(EXACT(D2, "OK")), "", TRIM(CLEAN(SUBSTITUTE(B2, CHAR(160), " "))))

Keep CLEAN even if you only use Google Sheets. It makes the workbook give the same result in Excel, and it removes control characters that sit in the middle of a code, such as the interior tab in Step 1.

Clean the master keys too

Step 4 in the comparison table changes two things at once. It looks up the cleaned order key in F, and it looks it up against cleaned master keys in Q instead of the raw master keys in O. A dirty master breaks a lookup just as easily as a dirty order. In a separate test, all eight order codes were clean and one master code, O2, had a trailing space. The raw lookup matched six orders worth $9,600.00. Matching against the cleaned master in Q found all eight.

Column Q (Master_Clean_Key) applies the same cleaning formula to each master code in O. The original master codes stay untouched in O.

Column R (Master_Row_Check) checks each master row before any lookup runs. The code cannot be made only of ASCII spaces, ASCII control characters 0 to 31, non-breaking spaces, or U+200B zero-width spaces. The region must be one of five exact labels: North, South, East, West, or Central. A region typed as “North ” with a trailing space fails, and so does a region made only of a zero-width space. If you reuse this worksheet with your own territories, replace the five quoted labels in R2 with your list and fill R2 down through R6.

Column S (Master_Clean_Key_Freq) counts how often each cleaned master code appears, ignoring uppercase and lowercase. This catches a problem that cleaning itself creates. Suppose the master lists “CUST-102” for South in O3 and “CUST-102 ” with a trailing space for Central in O6. In the raw master, those are two different codes. After cleaning, they are the same code with two regions. In the test, S3 and S6 both returned 2, column L stayed blank on every row, and AA14 stopped the report. The comparison columns I to K do not use this check. With this master, I3 still shows Central from the raw lookup, so never copy a region from those columns.

Google Sheets view of T11!O1:X6 where O6 contains CUST-102 with a trailing space assigned to Central while O3 holds CUST-102 for South, causing Master_Clean_Key_Freq in S3 and S6 to return 2 and forcing the four-stage comparison table in U1:X5 to return CHECK INPUTS.
In Google Sheets, a trailing space on O6 creates a post-clean CUST-102 collision with O3, raising Master_Clean_Key_Freq (S3 and S6) to 2 and halting the comparison block in U1:X5 to CHECK INPUTS before any region is assigned. Open full-size image.

The order rows get a matching check in column D. The order ID must be “ORD-” plus 2 to 6 digits, in capitals, with no spaces or wildcard characters. The amount must be zero or more with no more than two decimals. Both D and R reject a customer code made only of ASCII spaces, ASCII control characters 0 to 31, non-breaking spaces, or U+200B zero-width spaces. Without this check, an invisible code on both sides could match itself and assign real dollars to a region. Other invisible Unicode characters fall outside the check. In the test, a lone U+2060 word joiner passed it.

Step 3: Run XLOOKUP Against the Clean Keys and Audit Unmatched Dollars

Columns I to L run the four lookups side by side, so you can see what each step recovers on your own data. Each formula returns “NOT FOUND” when the code has no match:

  • I looks up the raw code in B against the raw master codes in O.
  • J looks up TRIM(B2) against O.
  • K looks up TRIM(CLEAN(B2)) against O.
  • L looks up the cleaned code in F against the cleaned master codes in Q.

Column L has two kinds of gates. D works one order at a time: an order that fails it stays blank in L, while the other orders still get a region. R and S work on the whole master: if any master row fails R, or any cleaned master code repeats in S, L stays blank on every row. For example, if A3 holds BAD instead of an order ID, L3 is blank, L2 still shows North, and AA14 stops the report. AA14 is the verdict for the whole report, so read it before you copy any region from the sheet.

The comparison block in U1:X5 counts the matches in each column and sums their order amounts. It reads the actual lookup results, so it reflects what each method did on this sheet. On the baseline, Excel for the web shows the 3, 5, 7, and 8 matches from the first table. Google Sheets shows 7 matches and $9,800.00 in V3:W3.

Why IFERROR with a zero hides the problem

A common shortcut is to wrap a lookup in IFERROR and fall back to zero. Column M rebuilds it:

=IFERROR(IF(XLOOKUP(B2, $O$2:$O$6, $P$2:$P$6)<>"", C2, 0), 0)

With no fallback inside XLOOKUP, a missing code returns #N/A. The comparison passes that error up, and IFERROR replaces it with 0. Column M adds up to $4,500.00. The five dirty rows show $0.00, and nothing on the sheet looks broken. In the test, the same formula without IFERROR returned #N/A on the missing row B3 and $1,500.00 on the matched row B2. Changing the fallback from 0 to 99999 returned 99999 on B3 and left B2 at $1,500.00. That shows the zero comes from IFERROR itself, on the rows the lookup missed.

The fix is to give the lookup a visible fallback such as “NOT FOUND”, as columns I to L do. Then count those rows and their dollars. Never let a lookup failure turn into a zero amount.

Google Sheets view of T06!Z1:AC14 testing IFERROR_MASKED (8 matched rows, $4,500.00 reported total, 0 unmatched rows) where Source_Audit reconciles at $12,000.00 while Candidate_Audit_Verdict in AC14 returns STOP: IFERROR(..., 0) MASKED 5 #N/A LOOKUPS (-$7500.00 ZEROED; REPORTED $4500.00 VS $12000.00).
In Google Sheets, testing IFERROR_MASKED ($4,500.00 reported across 8 rows with 0 unmatched) in Z1:AC14 triggers a STOP verdict exposing -$7,500.00 zeroed out across 5 masked #N/A lookups. Open full-size image.

Read the audit verdicts

The audit block in Z1:AC14 has two halves. The source half, AA2:AA14, counts invalid rows, duplicate or colliding codes, trailing rows below the tables, changed order codes, and raw and clean misses. It also shows the total and the matched dollars at each step. AA14 gives the verdict. On the baseline export, it returns:

RECONCILED: ALL 8 ORDERS MATCHED ($12000.00; ASSIGNED REGIONS TO $7500.00 ACROSS 5 NEWLY MATCHED ORDERS)

Two of those counts answer different questions. AA5 counts order codes whose text changed during cleaning. The count of newly matched orders in AA14 counts orders the raw lookup missed and the clean lookup found. They can differ. In the dirty-master test from Step 2, AA5 showed 0 because no order code changed, while AA14 reported $2,400.00 across 2 newly matched orders.

The candidate half checks a figure someone wants to report. Type the method, matched orders, matched dollars, and unmatched orders into AC2:AC5, and keep the two guard cells AC6:AC7 at 0. AC8:AC14 compare those figures with the lookups on the sheet. Type plain values without currency symbols, and never use formulas in AC2:AC7, because AC8 rejects formula inputs.

Each row below lists six values. Enter them down the column, from AC2 to AC7, and leave out the Scenario column. If you copy a row from another sheet, paste it transposed. Type the text entries without quote marks.

Scenario AC2 (Method) AC3 (Matched) AC4 (Matched USD) AC5 (Unmatched) AC6 AC7
Initial untested NOT TESTED NOT TESTED NOT TESTED NOT TESTED 0 0
Full clean TRIM_CLEAN_SUB160 8 12000.00 0 0 0
1. Raw XLOOKUP RAW_XLOOKUP 3 4500.00 5 0 0
2a. TRIM only, Excel for the web TRIM_ONLY 5 7500.00 3 0 0
2b. TRIM only, Google Sheets TRIM_ONLY 7 9800.00 1 0 0
3. TRIM and CLEAN TRIM_CLEAN_ONLY 7 9800.00 1 0 0
IFERROR with zero IFERROR_MASKED 8 4500.00 0 0 0
Partial manual fix TRIM_CLEAN_SUB160 7 10600.00 1 0 0

AC14 returns these verdicts on the baseline export. Rows 2a and 2b only fit their own tool, because each tool’s TRIM benchmark differs. The 2b verdict is the expected result for those inputs. The others were observed in both tools, except 2a, which was observed in Excel for the web.

  • Initial untested: NOT TESTED: ENTER CANDIDATE REPORT TOTALS
  • Full clean: RECONCILED: ALL 8 ORDERS MATCHED ($12000.00; $0.00 UNMATCHED)
  • 1. Raw XLOOKUP: STOP: RAW XLOOKUP LEFT 5 ORDERS WITHOUT A REGION (-$7500.00 UNMATCHED; $4500.00 VS $12000.00)
  • 2a. TRIM only, Excel for the web: STOP: TRIM() ALONE LEFT 3 ORDERS UNMATCHED (-$4500.00 UNMATCHED; $7500.00 VS $12000.00)
  • 2b. TRIM only, Google Sheets: STOP: TRIM() ALONE LEFT 1 ORDER UNMATCHED (-$2200.00 UNMATCHED; $9800.00 VS $12000.00)
  • 3. TRIM and CLEAN: STOP: TRIM(CLEAN()) LEFT 1 ORDER UNMATCHED (-$2200.00 UNMATCHED; $9800.00 VS $12000.00; INSPECT BOTH SIDES)
  • IFERROR with zero: STOP: IFERROR(..., 0) MASKED 5 #N/A LOOKUPS (-$7500.00 ZEROED; REPORTED $4500.00 VS $12000.00)
  • Partial manual fix: STOP: CANDIDATE MATCHED TOTAL DIFFERS BY -$1400.00 (1 ORDER UNMATCHED)
Excel for the web view of T05!Z1:AC14 testing TRIM_CLEAN_ONLY (7 matched orders, $9,800.00 matched dollars, 1 unmatched order) showing Candidate_Matched_Gap_USD of -$2,200.00 and Candidate_Audit_Verdict in AC14 returning STOP: TRIM(CLEAN()) LEFT 1 ORDER UNMATCHED (-$2200.00 UNMATCHED; $9800.00 VS $12000.00; INSPECT BOTH SIDES).
In Excel for the web, entering the TRIM and CLEAN totals (7 orders, $9,800.00 matched, 1 unmatched) triggers a STOP verdict flagging -$2,200.00 unmatched and prompting you to inspect both sides. Open full-size image.

If you enter the 2a figures in Google Sheets, AC14 stops with STOP: CANDIDATE DOES NOT MATCH TRIM-ONLY ($9800.00), because the sheet’s own TRIM lookup matched more.

The TRIM and CLEAN verdict says “inspect both sides” on purpose. Here, the miss is the non-breaking space in B5. When a dirty master causes the miss instead, the same verdict appears, and the fix belongs in the master.

To test the source guards, load the full clean inputs first (TRIM_CLEAN_SUB160, 8, 12000.00, 0, 0, and 0 in AC2:AC7). Then work on a duplicate tab, or restore each cell before the next test:

  • Change B2 to CUST-999, a code the master does not have. AA14 stops with STOP: 1 ORDER REMAINS UNMATCHED AFTER CLEANING ($1500.00; CHECK MASTER TABLE). Restore B2 to CUST-101.
  • Change A3 to ORD-7801. AA14 and AC14 stop with STOP: DUPLICATE ORDER IDS OR POST-CLEAN MASTER KEY COLLISION DETECTED. Restore A3 to ORD-7802.
  • Paste a ninth order into A10:C10 without resizing the ranges. AA14 and AC14 stop with STOP: TRAILING ROWS BELOW SOURCE OR MASTER TABLE (RESIZE RANGES). Clear A10:C10, and both return to RECONCILED.

How to Set Up and Resize the Worksheet

Everything runs on one worksheet named Clean_Keys, in four blocks separated by blank spacer columns N, T, and Y:

  1. Block 1 (orders and helpers, A1:M9): Enter the headings order_id, raw_customer_code, and order_amount_usd in A1:C1, and the eight orders in rows 2 to 9. Type the three clean codes in B2, B8, and B9 normally. To create the five dirty codes, type these formulas into a spare cell range such as AE2:AE6: ="CUST-102 "&"", =" CUST-103", ="CUST-104"&CHAR(160), ="CUST-105"&CHAR(10), and ="CUST-101"&CHAR(9). Copy them and paste them into B3:B7 as values only. In Google Sheets, Ctrl+Shift+V pastes values only. Then clear the spare cells. Each of B3:B7 should have a length of 9. Add the helper headings Row_Check, Order_ID_Freq, Clean_Key, Key_Changed_Flag, Edge_Char_Code, Raw_XLOOKUP, TRIM_XLOOKUP, TRIM_CLEAN_XLOOKUP, Clean_XLOOKUP, and IFERROR_Raw_Amount in D1:M1.
  2. Block 2 (customer master and guards, O1:S6): Enter customer_code, region_name, Master_Clean_Key, Master_Row_Check, and Master_Clean_Key_Freq in O1:S1. Then enter CUST-101 to CUST-105 in O2:O6, with North, South, East, West, and Central in P2:P6.
  3. Block 3 (comparison, U1:X5): Enter Cleaning_Stage, Matched_Rows, Matched_USD, and Unmatched_USD in U1:X1, and the stage labels 1_Raw_XLOOKUP, 2_TRIM_Only, 3_TRIM_CLEAN_Only, and 4_TRIM_CLEAN_SUB160 in U2:U5.
  4. Block 4 (audit, Z1:AC14): Enter Source_Audit_Metric, Source_Audit_Value, Candidate_Audit_Metric, and Candidate_Audit_Value in Z1:AC1, and the labels from the row map below in Z2:Z14 and AB2:AB14. Formulas point at cell positions, not label text, so you can rename the labels. Put NOT TESTED in AC2:AC5 (typed without quote marks) and 0 in AC6:AC7.
  5. Paste the 45 formulas from the reference at the end. Enter D2:M2 and fill down through row 9 (80 cells). Enter Q2:S2 and fill down through row 6 (15 cells). Then enter V2:X5 (12 cells), AA2:AA14 (13 cells), and AC8:AC14 (7 cells). That makes 127 formula cells. Eight sampled cells (D2, F2, I2, L2, M2, R2, AA14, and AC8) showed no _xlfn. prefixes in the Excel Formula Bar.
Row Source label (Z) Candidate label (AB) Baseline AC input
2 Invalid_Order_Or_Master_Rows Candidate_Clean_Method NOT TESTED
3 Duplicate_Or_Colliding_Keys Candidate_Matched_Rows NOT TESTED
4 Trailing_Rows_Below_Tables Candidate_Matched_USD NOT TESTED
5 Changed_Key_Rows Candidate_Unmatched_Rows NOT TESTED
6 Raw_Unmatched_Rows Candidate_Guard_Invalid_Rows 0
7 Clean_Unmatched_Rows Candidate_Guard_Trailing_Rows 0
8 Total_Orders_USD Candidate_Matched_Gap_USD (formula)
9 Raw_Matched_USD Candidate_Matched_Rows_Diff (formula)
10 TRIM_Only_Matched_USD Bench_Raw_Matched_USD (formula)
11 TRIM_CLEAN_Matched_USD Bench_TRIM_Matched_USD (formula)
12 Clean_Matched_USD Bench_TRIM_CLEAN_USD (formula)
13 IFERROR_Raw_Masked_USD Bench_IFERROR_Masked_USD (formula)
14 Source_Audit_Verdict Candidate_Audit_Verdict (formula)

Keep anything else out of columns A:C below the orders, out of O:P below row 6, and out of U below row 5. AA4 counts those cells as trailing rows and stops the report.

Resizing for more or fewer orders

Every formula that points at the order rows uses a column-qualified endpoint, :$A$9 through :$M$9. A Find and Replace on those endpoints never touches the master endpoints in row 6 or the audit cells. Call your current last row R_prev (9 in the baseline) and your new last row R_new (the order count plus 1).

  • When expanding: Paste the new orders directly below the current last row in columns A:C. Select D2:AC14, or down to row R_prev if that is lower. Run Find and Replace for each endpoint from :$A$ through :$M$ followed by R_prev, replacing it with the same column followed by R_new. Then fill D2:M2 down through row R_new.
  • When shrinking (at least one order): Run Find and Replace the same way first. Then clear the old inputs and helper formulas in columns A:M from row R_new + 1 through row R_prev, so AA4 returns 0.

The two tools handle Find and Replace differently:

  • Google Sheets (Edit > Find and replace): Check Also search within formulas before you click Replace all. With the box unchecked, the test found no matches for :$A$9. With it checked, the replacement went through.
  • Excel for the web (Home > Find & Select > Replace): There is no separate formula checkbox. Search options shows Within: Sheet, Match case, and Match entire cell contents, and Replace all updates the formula references directly.

If your Excel version has no XLOOKUP

Microsoft lists XLOOKUP for Microsoft 365, Excel 2021 and later, and Excel for the web (Microsoft Excel XLOOKUP). This worksheet needs one of those, or Google Sheets. In Excel 2016 or 2019, people often use INDEX with MATCH instead. Know one trap first: MATCH with a match type of 0 treats ? and * as wildcards, and the tilde (~) is the escape character for searching for those characters literally (Microsoft Excel MATCH). XLOOKUP matches exact text by default.

These two formulas demonstrate the trap, with the lookup code in B2 and the raw master in O2:P6:

=IFERROR(INDEX($P$2:$P$6, MATCH(B2, $O$2:$O$6, 0)), "NOT FOUND")

=IFERROR(INDEX($P$2:$P$6, MATCH(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B2, "~", "~~"), "*", "~*"), "?", "~?"), $O$2:$O$6, 0)), "NOT FOUND")

In the test, with “CUST-10?” as the code, XLOOKUP returned “NOT FOUND”. The first formula returned North, because the question mark matched the 1 in CUST-101. The second formula puts a tilde before each wildcard character, so it returned “NOT FOUND”, as it should. The test ran in Google Sheets and Excel for the web, not in desktop Excel 2016 or 2019.

These formulas only demonstrate the wildcard trap. They are not a drop-in replacement for column L, because they skip the row, master, and collision checks. With the two CUST-102 master rows from Step 2, an escaped INDEX and MATCH on the cleaned codes returns South, the first match, while L3 stays blank and AA14 stops. If you must use INDEX and MATCH, keep your codes to letters, digits, and hyphens, and keep a uniqueness check on the cleaned master codes.

Three Quick Questions

What if a key still returns “NOT FOUND” when columns G and H both show 0?

Look for a character the cleaning formula does not remove. A common one is the zero-width space, U+200B. It survives TRIM, CLEAN, and the CHAR(160) swap, so G and H both stay 0. Compare LEN(F2) with the length you expect. “CUST-101” with a zero-width space returns 9 instead of 8, and =UNICODE(RIGHT(F2, 1)) returns 8203 (Microsoft Excel UNICODE, Google Sheets UNICODE). For a character in the middle, use =UNICODE(MID(F2, 5, 1)) and change the 5 to each position you want to check. To remove it, add a second SUBSTITUTE inside the cleaning chain, so the zero-width space is gone before TRIM runs. For the order key in F2, which reads B2:

=IF(NOT(EXACT(D2, "OK")), "", TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(B2, UNICHAR(8203), ""), CHAR(160), " "))))

For the master key in Q2, which reads O2:

=IF(OR(ISERROR(O2), NOT(ISTEXT(O2))), "", TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(O2, UNICHAR(8203), ""), CHAR(160), " "))))

Fill both down as before. The order matters. If you remove the zero-width space after TRIM, a code like “CUST-101”, a space, and a zero-width space ends up with a trailing space. After the change, G returns 1 for a code that had a zero-width space. Column S still rechecks master uniqueness, so two master codes that differ only by a zero-width space now count as a collision and stop the report. This variant was not part of the tested workbook. It was checked only in a formula replay, so test it on your own data before you rely on it.

The two tools behaved differently here. In Excel for the web, the key returned “NOT FOUND” and the audit stopped. In Google Sheets, XLOOKUP ignored the zero-width space and returned North, while LEN still showed 9 and EXACT still showed FALSE. The hidden character stays in the cell, so it can break the same lookup later in Excel or in another system. Use LEN and UNICODE to find it, even when the lookup succeeds.

Should I clean keys in place or in a helper column?

Use a helper column. Keep the raw export untouched in B and point the lookup at Clean_Key in F. If another system needs the cleaned codes, copy F as values into a separate export or a new column after the audit returns RECONCILED. Do not paste them over B. Columns G and H and the before-and-after comparison all read the raw codes, so B is your evidence of what the export actually contained.

Does XLOOKUP care about uppercase and lowercase?

No. XLOOKUP treats “cust-101” and “CUST-101” as the same code. That is why column S uses UPPER when it checks for duplicate master codes. Two master codes that differ only in case would match the same orders, so the sheet treats them as a collision.

What to Do Next

Run this check on every lookup key right after your standard cleaning pass (a repeatable workflow for cleaning business spreadsheet data), and before any regional total goes into a report. If amounts in the same export also look short, see Fix Numbers Stored as Text in Excel and Google Sheets Before SUM Ignores Them. For a quick check on where spreadsheet habits and data definitions break down across your reporting, try the 4-Minute Small Business Data Audit.

Copy-Paste Formula Reference

Paste these 45 formulas into the Clean_Keys worksheet after loading A1:C9, O1:P6, the headings in D1:M1, Q1:S1, U1:X1, and Z1:AC1, the labels in U2:U5, Z2:Z14, and AB2:AB14, and the inputs in AC2:AC7 (NOT TESTED in AC2:AC5, 0 in AC6:AC7). Every formula uses functions supported by both Google Sheets and Excel for the web, including XLOOKUP, TRIM, CLEAN, SUBSTITUTE, EXACT, CODE, UNICHAR, COUNTIFS, SUMIFS, SUMPRODUCT, and ISFORMULA.

Block 1: Order helpers (enter in row 2, fill down through row 9)

D2, row check:

=IF(OR(COUNTA(A2:C2)<3, NOT(ISTEXT(A2)), NOT(ISTEXT(B2)), NOT(ISNUMBER(C2))), "CHECK ROW", IF(OR(TRIM(SUBSTITUTE(A2, CHAR(160), " "))="", NOT(EXACT(A2, UPPER(TRIM(SUBSTITUTE(A2, CHAR(160), " "))))), NOT(ISERROR(FIND(" ", A2))), NOT(ISERROR(FIND(CHAR(10), A2))), NOT(ISERROR(FIND(CHAR(13), A2))), NOT(ISERROR(FIND("*", A2))), NOT(ISERROR(FIND("?", A2))), NOT(ISERROR(FIND("~", A2))), LEN(A2)<6, LEN(A2)>10, NOT(EXACT(LEFT(A2, 4), "ORD-")), NOT(ISNUMBER(FIND(MID(A2, 5, 1), "0123456789"))), NOT(ISNUMBER(FIND(MID(A2, 6, 1), "0123456789"))), IF(LEN(A2)>=7, NOT(ISNUMBER(FIND(MID(A2, 7, 1), "0123456789"))), FALSE), IF(LEN(A2)>=8, NOT(ISNUMBER(FIND(MID(A2, 8, 1), "0123456789"))), FALSE), IF(LEN(A2)>=9, NOT(ISNUMBER(FIND(MID(A2, 9, 1), "0123456789"))), FALSE), IF(LEN(A2)=10, NOT(ISNUMBER(FIND(MID(A2, 10, 1), "0123456789"))), FALSE), TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(B2, CHAR(160), " "), UNICHAR(8203), "")))="", C2<0, ROUND(C2, 2)<>C2), "CHECK ROW", "OK"))

E2, order ID frequency:

=IF(OR(NOT(EXACT(D2, "OK")), COUNTIF($D$2:$D$9, "OK")<ROWS($A$2:$A$9)), "", SUMPRODUCT(--(UPPER($A$2:$A$9)=UPPER(A2))))

F2, cleaned customer code:

=IF(NOT(EXACT(D2, "OK")), "", TRIM(CLEAN(SUBSTITUTE(B2, CHAR(160), " "))))

G2, key changed flag:

=IF(NOT(EXACT(D2, "OK")), "", IF(EXACT(B2, F2), 0, 1))

H2, edge character code:

=IF(NOT(EXACT(D2, "OK")), "", IF(EXACT(B2, F2), 0, IF(CODE(LEFT(B2, 1))<>CODE(LEFT(F2, 1)), CODE(LEFT(B2, 1)), IF(CODE(RIGHT(B2, 1))<>CODE(RIGHT(F2, 1)), CODE(RIGHT(B2, 1)), "INTERIOR"))))

I2, raw lookup:

=IF(OR(NOT(EXACT(D2, "OK")), COUNTIF($R$2:$R$6, "<>OK")>0), "", XLOOKUP(B2, $O$2:$O$6, $P$2:$P$6, "NOT FOUND"))

J2, TRIM-only lookup:

=IF(OR(NOT(EXACT(D2, "OK")), COUNTIF($R$2:$R$6, "<>OK")>0), "", XLOOKUP(TRIM(B2), $O$2:$O$6, $P$2:$P$6, "NOT FOUND"))

K2, TRIM and CLEAN lookup:

=IF(OR(NOT(EXACT(D2, "OK")), COUNTIF($R$2:$R$6, "<>OK")>0), "", XLOOKUP(TRIM(CLEAN(B2)), $O$2:$O$6, $P$2:$P$6, "NOT FOUND"))

L2, full clean lookup against cleaned master:

=IF(OR(NOT(EXACT(D2, "OK")), COUNTIF($R$2:$R$6, "<>OK")>0, COUNTIF($S$2:$S$6, ">1")>0), "", XLOOKUP(F2, $Q$2:$Q$6, $P$2:$P$6, "NOT FOUND"))

M2, IFERROR zero-fallback amount (the trap, kept for comparison):

=IF(OR(NOT(EXACT(D2, "OK")), COUNTIF($R$2:$R$6, "<>OK")>0), "", IFERROR(IF(XLOOKUP(B2, $O$2:$O$6, $P$2:$P$6)<>"", C2, 0), 0))

Block 2: Master helpers (enter in row 2, fill down through row 6)

Q2, cleaned master code:

=IF(OR(ISERROR(O2), NOT(ISTEXT(O2))), "", TRIM(CLEAN(SUBSTITUTE(O2, CHAR(160), " "))))

R2, master row check (replace the five region labels for your own territories):

=IF(OR(ISERROR(O2), ISERROR(P2), NOT(ISTEXT(O2)), NOT(ISTEXT(P2))), "CHECK MASTER", IF(OR(Q2="", TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(O2, CHAR(160), " "), UNICHAR(8203), "")))="", NOT(OR(EXACT(P2, "North"), EXACT(P2, "South"), EXACT(P2, "East"), EXACT(P2, "West"), EXACT(P2, "Central")))), "CHECK MASTER", "OK"))

S2, cleaned master code frequency:

=IF(OR(NOT(EXACT(R2, "OK")), COUNTIF($R$2:$R$6, "<>OK")>0), "", SUMPRODUCT(--(UPPER($Q$2:$Q$6)=UPPER(Q2))))

Block 3: Four-step comparison (V2:X5)

V2, raw matched orders:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", COUNTIF($I$2:$I$9, "<>NOT FOUND"))

W2, raw matched dollars:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", ROUND(SUMIFS($C$2:$C$9, $I$2:$I$9, "<>NOT FOUND"), 2))

X2, raw unmatched dollars:

=IF(NOT(ISNUMBER(W2)), "CHECK INPUTS", ROUND(AA8-W2, 2))

V3, TRIM-only matched orders:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", COUNTIF($J$2:$J$9, "<>NOT FOUND"))

W3, TRIM-only matched dollars:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", ROUND(SUMIFS($C$2:$C$9, $J$2:$J$9, "<>NOT FOUND"), 2))

X3, TRIM-only unmatched dollars:

=IF(NOT(ISNUMBER(W3)), "CHECK INPUTS", ROUND(AA8-W3, 2))

V4, TRIM and CLEAN matched orders:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", COUNTIF($K$2:$K$9, "<>NOT FOUND"))

W4, TRIM and CLEAN matched dollars:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", ROUND(SUMIFS($C$2:$C$9, $K$2:$K$9, "<>NOT FOUND"), 2))

X4, TRIM and CLEAN unmatched dollars:

=IF(NOT(ISNUMBER(W4)), "CHECK INPUTS", ROUND(AA8-W4, 2))

V5, full clean matched orders:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", COUNTIF($L$2:$L$9, "<>NOT FOUND"))

W5, full clean matched dollars:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", ROUND(SUMIFS($C$2:$C$9, $L$2:$L$9, "<>NOT FOUND"), 2))

X5, full clean unmatched dollars:

=IF(NOT(ISNUMBER(W5)), "CHECK INPUTS", ROUND(AA8-W5, 2))

Block 4A: Source audit (AA2:AA14)

AA2, invalid order or master rows:

=COUNTIF($D$2:$D$9, "<>OK")+COUNTIF($R$2:$R$6, "<>OK")

AA3, duplicate order IDs or colliding master codes:

=IF(AA2>0, 0, COUNTIF($E$2:$E$9, ">1")+COUNTIF($S$2:$S$6, ">1"))

AA4, trailing rows below the tables:

=COUNTA($A:$C)-COUNTA($A$1:$C$9)+COUNTA($O:$P)-COUNTA($O$1:$P$6)+COUNTA($U:$U)-COUNTA($U$1:$U$5)

AA5, changed order codes:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", COUNTIF($G$2:$G$9, 1))

AA6, raw unmatched orders:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", COUNTIF($I$2:$I$9, "NOT FOUND"))

AA7, clean unmatched orders:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", COUNTIF($L$2:$L$9, "NOT FOUND"))

AA8, total order dollars:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", ROUND(SUM($C$2:$C$9), 2))

AA9, raw matched dollars:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", W2)

AA10, TRIM-only matched dollars:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", W3)

AA11, TRIM and CLEAN matched dollars:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", W4)

AA12, full clean matched dollars:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", W5)

AA13, IFERROR-masked total:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", ROUND(SUM($M$2:$M$9), 2))

AA14, source audit verdict:

=IF(OR(AA2>0, COUNTBLANK($A$2:$C$9)>0, COUNTBLANK($O$2:$P$6)>0), "STOP: INVALID OR BLANK ORDER OR MASTER ROWS", IF(AA3>0, "STOP: DUPLICATE ORDER IDS OR POST-CLEAN MASTER KEY COLLISION DETECTED", IF(AA4>0, "STOP: TRAILING ROWS BELOW SOURCE OR MASTER TABLE (RESIZE RANGES)", IF(AA7>0, "STOP: " & AA7 & " ORDER" & IF(AA7=1, " REMAINS", "S REMAIN") & " UNMATCHED AFTER CLEANING ($" & TEXT(X5, "0.00") & "; CHECK MASTER TABLE)", "RECONCILED: ALL " & ROWS($A$2:$A$9) & " ORDERS MATCHED ($" & TEXT(AA12, "0.00") & "; ASSIGNED REGIONS TO $" & TEXT(ROUND(AA12-AA9, 2), "0.00") & " ACROSS " & COUNTIFS($I$2:$I$9, "NOT FOUND", $L$2:$L$9, "<>NOT FOUND") & " NEWLY MATCHED ORDER" & IF(COUNTIFS($I$2:$I$9, "NOT FOUND", $L$2:$L$9, "<>NOT FOUND")=1, "", "S") & ")"))))

Block 4B: Candidate report audit (AC8:AC14)

AC8, candidate matched-dollar gap:

=IF(OR(LEFT(AA14, 5)="STOP:", ISFORMULA(AC2), ISFORMULA(AC3), ISFORMULA(AC4), ISFORMULA(AC5), ISFORMULA(AC6), ISFORMULA(AC7), NOT(ISNUMBER(AC6)), NOT(ISNUMBER(AC7))), "CHECK INPUTS", IF(OR(INT(AC6)<>AC6, AC6<>0, INT(AC7)<>AC7, AC7<>0), "CHECK INPUTS", IF(IF(AND(ISTEXT(AC2), ISTEXT(AC3), ISTEXT(AC4), ISTEXT(AC5)), AND(EXACT(AC2, "NOT TESTED"), EXACT(AC3, "NOT TESTED"), EXACT(AC4, "NOT TESTED"), EXACT(AC5, "NOT TESTED")), FALSE), "NOT TESTED", IF(OR(NOT(ISTEXT(AC2)), NOT(ISNUMBER(AC3)), NOT(ISNUMBER(AC4)), NOT(ISNUMBER(AC5))), "CHECK INPUTS", IF(OR(INT(AC3)<>AC3, AC3<0, AC4<0, ROUND(AC4, 2)<>AC4, INT(AC5)<>AC5, AC5<0, AC3+AC5<>ROWS($A$2:$A$9)), "CHECK INPUTS", ROUND(AC4-AA8, 2))))))

AC9, candidate matched-order difference:

=IF(EXACT(AC8, "NOT TESTED"), "NOT TESTED", IF(NOT(ISNUMBER(AC8)), "CHECK INPUTS", AC3-ROWS($A$2:$A$9)))

AC10, raw lookup benchmark:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", AA9)

AC11, TRIM-only benchmark:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", AA10)

AC12, TRIM and CLEAN benchmark:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", AA11)

AC13, IFERROR benchmark:

=IF(OR(AA2>0, AA3>0, AA4>0), "CHECK INPUTS", AA13)

AC14, candidate audit verdict:

=IF(LEFT(AA14, 5)="STOP:", AA14, IF(IF(AND(NOT(ISFORMULA(AC6)), NOT(ISFORMULA(AC7)), ISNUMBER(AC6), ISNUMBER(AC7)), OR(INT(AC6)<>AC6, AC6<>0, INT(AC7)<>AC7, AC7<>0), TRUE), "STOP: INVALID CANDIDATE GUARD INPUTS", IF(EXACT(AC8, "NOT TESTED"), "NOT TESTED: ENTER CANDIDATE REPORT TOTALS", IF(EXACT(AC8, "CHECK INPUTS"), "STOP: INVALID OR NON-NUMERIC CANDIDATE REPORT INPUTS", IF(NOT(OR(EXACT(AC2, "TRIM_CLEAN_SUB160"), EXACT(AC2, "RAW_XLOOKUP"), EXACT(AC2, "TRIM_ONLY"), EXACT(AC2, "TRIM_CLEAN_ONLY"), EXACT(AC2, "IFERROR_MASKED"))), "STOP: UNKNOWN CANDIDATE CLEAN METHOD", IF(EXACT(AC2, "RAW_XLOOKUP"), IF(OR(AC3<>V2, ROUND(AC4-AC10, 2)<>0), "STOP: CANDIDATE DOES NOT MATCH RAW XLOOKUP ($" & TEXT(AC10, "0.00") & ")", IF(AND(AC5=0, AC8=0), "RECONCILED: ALL " & AC3 & " ORDERS ALREADY MATCHED RAW ($" & TEXT(AC4, "0.00") & "; $0.00 UNMATCHED)", "STOP: RAW XLOOKUP LEFT " & AC5 & " ORDER" & IF(AC5=1, "", "S") & " WITHOUT A REGION (" & IF(AC8<0, "-$", "+$") & TEXT(ABS(AC8), "0.00") & " UNMATCHED; $" & TEXT(AC4, "0.00") & " VS $" & TEXT(AA8, "0.00") & ")")), IF(EXACT(AC2, "TRIM_ONLY"), IF(OR(AC3<>V3, ROUND(AC4-AC11, 2)<>0), "STOP: CANDIDATE DOES NOT MATCH TRIM-ONLY ($" & TEXT(AC11, "0.00") & ")", IF(AND(AC5=0, AC8=0), "RECONCILED: TRIM() MATCHED ALL " & AC3 & " ORDERS ($" & TEXT(AC4, "0.00") & "; $0.00 UNMATCHED)", "STOP: TRIM() ALONE LEFT " & AC5 & " ORDER" & IF(AC5=1, "", "S") & " UNMATCHED (" & IF(AC8<0, "-$", "+$") & TEXT(ABS(AC8), "0.00") & " UNMATCHED; $" & TEXT(AC4, "0.00") & " VS $" & TEXT(AA8, "0.00") & ")")), IF(EXACT(AC2, "TRIM_CLEAN_ONLY"), IF(OR(AC3<>V4, ROUND(AC4-AC12, 2)<>0), "STOP: CANDIDATE DOES NOT MATCH TRIM+CLEAN ($" & TEXT(AC12, "0.00") & ")", IF(AND(AC5=0, AC8=0), "RECONCILED: TRIM(CLEAN()) MATCHED ALL " & AC3 & " ORDERS ($" & TEXT(AC4, "0.00") & "; $0.00 UNMATCHED)", "STOP: TRIM(CLEAN()) LEFT " & AC5 & " ORDER" & IF(AC5=1, "", "S") & " UNMATCHED (" & IF(AC8<0, "-$", "+$") & TEXT(ABS(AC8), "0.00") & " UNMATCHED; $" & TEXT(AC4, "0.00") & " VS $" & TEXT(AA8, "0.00") & "; INSPECT BOTH SIDES)")), IF(EXACT(AC2, "IFERROR_MASKED"), IF(OR(AC3<>ROWS($A$2:$A$9), AC5<>0, ROUND(AC4-AC13, 2)<>0), "STOP: CANDIDATE DOES NOT MATCH IFERROR-MASKED TOTAL ($" & TEXT(AC13, "0.00") & ")", IF(AA6=0, "RECONCILED: NO #N/A LOOKUPS OCCURRED TO MASK ($" & TEXT(AC4, "0.00") & ")", "STOP: IFERROR(..., 0) MASKED " & AA6 & " #N/A LOOKUP" & IF(AA6=1, "", "S") & " (" & IF(AC8<0, "-$", "+$") & TEXT(ABS(AC8), "0.00") & " ZEROED; REPORTED $" & TEXT(AC4, "0.00") & " VS $" & TEXT(AA8, "0.00") & ")")), IF(OR(AC8<>0, AC5<>0), "STOP: CANDIDATE MATCHED TOTAL DIFFERS BY " & IF(AC8<0, "-$", "+$") & TEXT(ABS(AC8), "0.00") & " (" & AC5 & " ORDER" & IF(AC5=1, "", "S") & " UNMATCHED)", "RECONCILED: ALL " & AC3 & " ORDERS MATCHED ($" & TEXT(AC4, "0.00") & "; $0.00 UNMATCHED)"))))))))))

Continue Reading

Email Notifications

Get notified when new Practical Analytics articles are published.

Field notes on table grain, trustworthy KPIs, and AI-assisted data workflows for small and medium businesses. Sent only when a new guide goes live.

No spam or automated drip funnels. Unsubscribe anytime.