Excel 用自定義公式做資料驗證

選中輸入單元格,開啟“資料 > 資料驗證”,將允許條件設為自定義,並輸入在允許該值時返回 TRUE 的公式。公式按所選區域的第一個單元格來寫,例如區域從 B2 開始時用 =AND(B2>=1,B2<=10)。自定義規則不會顯示下拉箭頭,需要列表時請用“序列”。

本文目錄

快速解答

選中輸入單元格,開啟“資料 > 資料驗證”,將允許條件設為自定義,並輸入在允許該值時返回 TRUE 的公式。公式按所選區域的第一個單元格來寫,例如區域從 B2 開始時用 =AND(B2>=1,B2<=10)。自定義規則不會顯示下拉箭頭,需要列表時請用“序列”。

=AND(B2>=1,B2<=10)

檢查重點

2026 年 11 月 12 日測試中,AND 對 5 為 TRUE,對 0 和 11 為 FALSE。ISNUMBER(5) 為 TRUE,對文本 n/a 為 FALSE。COUNTIF 對 East 為 1,對 South 為 0。防重複可用 =COUNTIF($B$2:$B$20,B2)=1。請先在輔助列測試同一公式。

邊界與相容性

公式必須以等號開頭,結果必須是 TRUE 或 FALSE。忽略空值僅在允許空白時勾選。自定義規則不會顯示箭頭,這是正常的。貼上的值也會被檢查。每個單元格只能有一條驗證規則,需要下拉又要額外條件時,應把邏輯寫進同一公式,或用輔助列另行檢查。

完整英文範例、公式與作者備註

Tested in: The TRUE/FALSE and COUNTIF checks below were evaluated in Google Sheets on 12 November 2026. Excel 365 menu names are the current desktop labels; I did not click through Data Validation in Excel this session.

A plain list is covered in How to Add a Drop-Down List in Excel. This page is about the Custom option.

Write the formula for the first input cell

If you selected B2:B20, the formula is evaluated as if it were entered in B2, then filled down. Relative references should point at B2, not at B1 or at a locked $B$2 unless every row must test the same cell.

A number between 1 and 10

=AND(B2>=1,B2<=10)

In the test workbook on 12 November 2026, =AND(5>=1,5<=10) was TRUE, and the same test for 0 and for 11 was FALSE.

A real number, not text

=ISNUMBER(B2)

=ISNUMBER(5) was TRUE. =ISNUMBER("n/a") was FALSE. Numbers stored as text fail this test and should be converted first; see convert text to numbers.

A value that already appears in a list (without using the List type):

=COUNTIF(Lists!A2:A20,B2)=1

COUNTIF of "East" against {East;West;North} returned 1, and "South" returned 0. A 0 is rejected. This is useful when you do not want a drop-down but still want the cell limited to known items. For an actual drop-down pointing at another sheet, use a List rule as in list from another sheet.

No duplicates in the input column

=COUNTIF($B$2:$B$20,B2)=1

The range is locked; the cell being tested is not. The first time a value appears, the count is 1 and the entry is allowed. A second paste of the same value becomes 2 and is rejected.

In Google Sheets the custom formula goes in the Custom formula is criterion of Data validation, with the same references.

Test it on the sheet before you open the dialog

Put the formula in an empty column on row 2, fill down, and confirm you see TRUE where an entry should be allowed and FALSE where it should be blocked. If the helper column is already wrong, the validation rule will be wrong too.

The formula must start with =. A result that is not TRUE or FALSE — a #VALUE!, a number, or a blank — is treated as a failed check, which feels like a rule that rejects everything.

Turn Ignore blank off if an empty cell should be rejected. Leave it on if the input is optional. Pasted values are also checked, which is the usual way a rule “suddenly” fires after a copy from another sheet.

If the arrow is missing, you are probably on a Custom rule, not a List rule. That is expected. If a List rule is the one that is misbehaving, use drop-down list not working instead of changing it to Custom.

Oscar

專注電子表格公式與排錯;具體測試環境和待驗證事項見各篇文章。

Tested in Google Sheets (via Sheets API) on 12 November 2026: AND(5>=1,5<=10) is TRUE, AND for 0 and for 11 are FALSE; ISNUMBER(5) is TRUE and ISNUMBER of the text n/a is FALSE; COUNTIF of East against {East;West;North} is 1 and South is 0. Excel 365 menu names are the current desktop labels; I did not click through Data Validation in Excel this session..

本主題下一篇:資料清理與錄入 Excel 下拉式清單不顯示、出現空白或缺少專案