Sub topic 3
Apply, copy, edit and remove rules
This chapter assumes you can select ranges, write formulas with absolute references such as $A$2, and refer to another sheet in a formula. You should already know how to open the Data validation panel from the Data menu.
Apply, copy, edit and remove rules
A rule applies to a range. To change it, select the range, open Data > Data validation, and edit the rule in the side panel. To copy a rule to other cells, use Edit > Paste special > Data validation only. A normal paste also replaces the rule on the destination cells, which surprises many people. To remove a rule, select the range and click Remove rule in the panel. Removing a rule leaves the cell values in place.
Custom formula rules
A custom formula rule returns TRUE for valid input. Set the criteria to "Custom formula is" and use a relative reference to the first cell of the range. To stop duplicates in A2:A100, use this formula:
=COUNTIF($A$2:$A$100, A2)=1Dependent drop-downs
A dependent drop-down changes its options based on another cell. Put a FILTER formula in a helper cell, such as Lists!E2, to return only the items for the category in A2. Then point the second drop-down at the helper range. The helper formula must return the items, so the drop-down's source range must cover the cells that hold them.
What validation does not protect against
Validation checks typed input only. Pasted values can overwrite a rule. Validation also does not check data that was already in the cells before you added the rule. Use a formula or a conditional format to flag those values.
Set validation rules with Apps Script
Apps Script can add and remove rules. This function applies a status drop-down to C2:C100:
function addStatusDropdown() {
const sheet = SpreadsheetApp.getActiveSheet();
const rule = SpreadsheetApp.newDataValidation()
.requireValueInList(['Not started', 'In progress', 'Done'], true)
.setAllowInvalid(false)
.setHelpText('Pick a status from the list.')
.build();
sheet.getRange('C2:C100').setDataValidation(rule);
}This function removes the rules from the same range without deleting values:
function clearStatusValidation() {
SpreadsheetApp.getActiveSheet().getRange('C2:C100').clearDataValidations();
}Exercise
You copy a drop-down from B2 to D2 with a normal paste. What happens to the drop-down in D2?
Show answer
The normal paste overwrites the validation in D2 with the rule from B2, and it also copies the value. To copy only the rule, use Paste special > Data validation only.