One way to prevent errors in data entry is to use the Data Validation tool. This tool allows us to apply a logical condition to a cell and reject any input that does not meet the condition.
- Allow entering a value into a cell if another cell is not empty.
- Allow entering only numbers into a cell if another cell is not empty.
Allow entering a value into a cell if another cell is not empty
For example, suppose we have a cell that requires a value only if another cell has a value. We can use the Data Validation tool to check if the date cell is empty or not, and only accept the value if it is not empty:
- Select the cell range you want to enter the data
- Click
Data > Data Validation
. - The Data Validation dialog box will be displayed.
- On the
Settings
tab, selectCustom
from theAllow
list. - Type
=A2:A5<>""
(any range you want to validate) into theFormula
box. - Click the
Error Alert
tab, enter a Title and Error Message - Click OK.
The =A2:A5<>""
formula checks if each cell in the range A2:A5
is empty or not. The formula uses the <>
operator, which means “not equal to”. So, the formula =A2:A5<>""
means “compare each cell in A2:A5
to an empty”.
When you attempt to enter a value into a cell in the Age
column without entering the value into the cell in the Name
column, the validation rule is triggered, and the error alert is shown:
Allow entering only numbers into a cell if another cell is not empty
- Select the cell range you want to enter the data
- Click
Data > Data Validation
. - The Data Validation dialog box will be displayed.
- On the
Settings
tab, selectCustom
from theAllow
list. - Type
=IF(A2:A5<>"",ISNUMBER(B2:B5),FALSE)
(or any range you want to validate) into theFormula
box. - Click the
Error Alert
tab, enter a Title and Error Message - Click OK.
The =IF(A2:A5<>"",ISNUMBER(B2:B5),FALSE)
formula is used to check if the cells in column B are numbers only and if the corresponding cells in column A are not empty.
The IF function takes three arguments:
- A logical test
The logical test isA2:A5<>"",
which means that the cells in the range are not blank. - a value if true
The value if true isISNUMBER(B2:B5)
, which returnsTRUE
orFALSE
depending on whether the cells in the range are numbers or not. - and a value if false.
The value if false isFALSE
, which means that if the cells in theA2:A5
are blank, the formula will returnFALSE
regardless of the values inB2:B5
.
Now, if you enter a non-number value into a cell in the Age
column without entering the value into the cell in the Name
column, you’ll get an error alert: