[Excel VBA for Beginners] How to Set Data Validation to Block Invalid Values | Using the Validation Property

目次

Introduction

Excel has a feature called “Data Validation” that restricts users to entering only specific formats or values.

By controlling this feature via VBA, you can automate input checks and set up rules efficiently when initializing a sheet.

In this article, I will explain how to set data validation rules on cells using VBA.

What is Data Validation?

Data Validation is an Excel feature that specifies “what kind of values are allowed” for a cell.

For example, you can restrict inputs to “future dates only,” “integers only,” or “selection from a list.”

How to Set Data Validation with VBA

The following code sets a validation rule for the entire Column B, allowing “only dates on or after January 1, 2025.”

Important: It is standard practice to execute .Validation.Delete before setting a new rule to prevent errors if a rule already exists.

Sub SetDateValidation()

    With Worksheets("Sheet1").Range("B:B")
        ' 1. Delete existing validation rules (to prevent errors)
        .Validation.Delete
        
        ' 2. Set new validation rule (Allow only dates from 2025/1/1 onwards)
        .Validation.Add _
            Type:=xlValidateDate, _
            Operator:=xlGreaterEqual, _
            Formula1:="2025/1/1"
            
        ' (Optional) Set an error message
        .Validation.ErrorMessage = "Please enter a date on or after January 1, 2025."
        .Validation.ShowError = True
    End With

End Sub

Argument: Type (Validation Type)

Specify the data type using the Type argument of the .Add method.

Constant NameDescription
xlValidateWholeNumberRestrict to whole numbers (integers)
xlValidateDecimalRestrict to decimal numbers
xlValidateListSelect from a list (Dropdown)
xlValidateDateRestrict to dates
xlValidateTimeRestrict to times
xlValidateTextLengthRestrict text length
xlValidateCustomDefined by a formula (Custom)
xlValidateInputOnlyNo validation, only show input message

Argument: Operator (Comparison Operator)

Specify the condition (greater than, less than, between, etc.) using the Operator argument of the .Add method.

Constant NameMeaning
xlBetweenBetween Formula1 and Formula2
xlNotBetweenNot between
xlEqualEqual to
xlNotEqualNot equal to
xlGreaterGreater than (>)
xlGreaterEqualGreater than or equal to (>=)
xlLessLess than (<)
xlLessEqualLess than or equal to (<=)

How to Delete Data Validation?

To clear existing validation rules, use .Validation.Delete.

Range("B:B").Validation.Delete

Example: Limiting Text Length (Max 10 Characters)

Here is an example to prevent input errors for items with a fixed length, such as IDs or codes.

With Range("C2:C100")
    .Validation.Delete
    .Validation.Add _
        Type:=xlValidateTextLength, _
        Operator:=xlLessEqual, _
        Formula1:="10"
    
    .Validation.ErrorMessage = "Please enter within 10 characters."
End With

Summary

ItemContent
How to SetUse the .Validation.Add method
How to DeleteUse the .Validation.Delete method
Main UsesDate restrictions, number ranges, text length limits, creating dropdown lists
NoteAlways run .Delete before setting to avoid errors

Setting flexible data validation with VBA contributes significantly to preventing user input errors and maintaining data integrity. Please try incorporating this when creating business Excel templates or macro-enabled workbooks.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次