跳到正文
DEV Community· SheetError·· 1 小時前AI 評分44

Google Sheets XLOOKUP #N/A「Did not find value」:先確認查找未命中,再使用 if_not_found

XLOOKUP #N/A "Did not find value" in Google Sheets: confirm the miss, then use if_not_found

AI 導讀

Google Sheets 的 XLOOKUP 在精確比對找不到鍵值時會回傳 #N/A,應先確認鍵值確實不存在,再決定是否設定未找到時的替代值。第四個參數 if_not_found 只處理查找未命中;IFNA 也可處理 #N/A,而 IFERROR 會連其他錯誤一併隱藏。若鍵值看似存在仍回傳 #N/A,應先檢查數字與文字格式或尾隨空格。

正文

You type =XLOOKUP(D2,A2:A3,B2:B3) and the cell shows #N/A. Hover it and the tooltip says Did not find value ‘999’ in XLOOKUP evaluation. The function ran. There was no match. That's the same error value as a VLOOKUP miss. Confirm the key is actually absent before you change match mode or wrap the formula.

Why it happens

XLOOKUP defaults to exact match (match_mode 0). If the search key is not in the lookup range, the result is #N/A. The formula is fine. The key isn't there — or it only looks like it is (a number versus text, or a trailing space).

When a miss is a valid result, the fourth argument (missing_value in Sheets; Excel callers know the same slot as if_not_found) replaces that #N/A only. A size mismatch, a #REF! range, or #DIV/0! inside a more complex formula still surfaces. That is the same idea as IFNA. IFERROR is broader: it will also hide those other errors.

Reproduce it in a minute

  1. Open a blank spreadsheet. In A1 type sku, B1 name. A2 101 as a number. B2 Widget. A3 102. B3 Gadget. D1 lookup. D2 999 — that sku is not in column A. D3 101.

  2. Exact miss. In E2 enter:

=XLOOKUP(D2,A2:A3,B2:B3)

E2 shows #N/A. The tooltip is Did not find value ‘999’ in XLOOKUP evaluation. Leave E2 broken.

  1. A hit on the same table. In E3 enter:
=XLOOKUP(D3,A2:A3,B2:B3)

E3 shows Widget. Same function, same ranges. D3 is on the sheet. That row is the control: XLOOKUP works when the key exists.

  1. if_not_found on the miss. In F2 enter:
=XLOOKUP(D2,A2:A,B2:B,"none")

F2 shows none. The fourth argument fired because 999 is missing. It did not change E3.

  1. IFNA around a bare XLOOKUP. In G2 enter:
=IFNA(XLOOKUP(D2,A2:A,B2:B),"none")

G2 shows none. Same fallback as F2. The wrapper sits outside the function. The fourth argument sits inside it.

  1. IFERROR also looks like a miss — until the range is wrong. In H2 enter:
=IFERROR(XLOOKUP(D2,A2:A3,B2:B3),"none")

H2 shows none. Then in I2 enter:

=XLOOKUP(D2,A2:Z2,B2:B3)

Lookup and result ranges are different sizes. I2 errors. In I3 enter:

=IFERROR(XLOOKUP(D2,A2:Z2,B2:B3),"none")

I3 shows none. In I4 enter:

=XLOOKUP(D2,A2:Z2,B2:B3,"none")

The fourth argument does not turn a broken range into none. The miss argument is the miss only.

  1. Confirm the miss before you wrap. Type 101 in D2. E2 becomes Widget. Put 999 back so #N/A returns. If changing the lookup value makes #N/A come and go, the function is working and the key is absent.

The fix

Leave E2 as the raw #N/A. Leave I2 as the broken range.

When a miss is not allowed. Put the sku on the table, or fix the key. Do not add if_not_found so a missing 999 looks like a real name. E3 already proves the formula works for 101.

When a miss is a valid result. Prefer the fourth argument:

=XLOOKUP(D2,A2:A,B2:B,"none")

That is if_not_found. You do not need IFNA around a function that already has a miss slot.

If you must wrap. =IFNA(XLOOKUP(D2,A2:A,B2:B),"none") matches F2 on this miss. Use it when the XLOOKUP is already written without a fourth argument and you do not want to touch the inner call. Do not upgrade that wrap to IFERROR “to be safe.”

Do not switch to VLOOKUP to clear #N/A. VLOOKUP has no miss argument. Stay on XLOOKUP. Fix the key, or pass if_not_found.

Watch out for

  • It's not a type mismatch until you check. XLOOKUP does not coerce number vs text. If D2 were text '101 and A2 were numeric 101, E2 would still be #N/A with the sku “on the sheet.” if_not_found will not fix type. Check ISNUMBER vs ISTEXT, and trailing spaces, before you wrap.
  • IFERROR hides bugs. Use the fourth argument when the only thing you want to hide is “this key is not in the list.” IFERROR will also swallow a #REF! from a deleted name, a #NAME? from XLOKUP, and a shape error. Those are bugs, not misses. if_not_found leaves them visible. So does IFNA.
  • An empty fourth argument is not a blank cell. A true blank fallback is a different recipe than "none".
  • Searching the wrong column is not this miss. =XLOOKUP(101,A2:A3,B2:B3) searches sku and returns name. If the name sits to the left of the key, you're looking in the wrong range, not hitting a missing key.
  • This is not FILTER with zero rows. FILTER also uses #N/A, but the tooltip is different (No matches are found or FILTER has mismatched range sizes). Read the tooltip first.
  • If the sku is on the sheet and XLOOKUP is still #N/A, stop wrapping. Check type and spaces first. Then, if the key is truly absent and a fallback is allowed, pass if_not_found.

Originally published on SheetError, a free library of Google Sheets error fixes (I run it).

This article was created with the help of AI.

來源:DEV Community · dev.to