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
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
Open a blank spreadsheet. In A1 type
sku, B1name. A2101as a number. B2Widget. A3102. B3Gadget. D1lookup. D2999— that sku is not in column A. D3101.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.
- 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.
-
if_not_foundon 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.
-
IFNAaround a bareXLOOKUP. 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.
-
IFERRORalso 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.
- Confirm the miss before you wrap. Type
101in D2. E2 becomes Widget. Put999back so#N/Areturns. If changing the lookup value makes#N/Acome 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.
XLOOKUPdoes not coerce number vs text. If D2 were text '101 and A2 were numeric101, E2 would still be#N/Awith the sku “on the sheet.”if_not_foundwill not fix type. CheckISNUMBERvsISTEXT, 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.”
IFERRORwill also swallow a#REF!from a deleted name, a#NAME?fromXLOKUP, and a shape error. Those are bugs, not misses.if_not_foundleaves them visible. So doesIFNA. -
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