Validation.Add 方法 (Excel)Validation.Add method (Excel)

向指定区域添加数据验证。Adds data validation to the specified range.

语法Syntax

表达式(TypeAlertStyleOperatorFormula1Formula2)expression.Add (Type, AlertStyle, Operator, Formula1, Formula2)

_表达式_一个代表**验证** 对象的变量。expression A variable that represents a Validation object.

参数Parameters

名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
TypeType 必需Required XlDVTypeXlDVType 有效性验证类型。The validation type.
AlertStyleAlertStyle 可选Optional VariantVariant 验证警报样式。The validation alert style. 可以是下列的**XlDVAlertStyle** 常量之一: xlValidAlertInformationxlValidAlertStopxlValidAlertWarningCan be one of the following XlDVAlertStyle constants: xlValidAlertInformation, xlValidAlertStop, or xlValidAlertWarning.
OperatorOperator 可选Optional VariantVariant 数据验证运算符。The data validation operator. 可以是下列的**XlFormatConditionOperator** 常量之一: xlBetweenxlEqualxlGreaterxlGreaterEqualxlLessxlLessEqualxlNotBetweenxlNotEqual.Can be one of the following XlFormatConditionOperator constants: xlBetween, xlEqual, xlGreater, xlGreaterEqual, xlLess, xlLessEqual, xlNotBetween, or xlNotEqual.
Formula1Formula1 可选Optional VariantVariant 数据验证公式中的第一部分。The first part of the data validation equation. 值不得超过 255 个字符。Value must not exceed 255 characters.
Formula2Formula2 可选Optional VariantVariant 当_Operator_为xlBetweenxlNotBetween时, 数据验证等式的第二部分 (否则, 将忽略此参数)。The second part of the data validation equation when Operator is xlBetween or xlNotBetween (otherwise, this argument is ignored).

备注Remarks

Add 方法所要求的参数依验证的类型而定,如下表所示。The Add method requires different arguments, depending on the validation type, as shown in the following table.

有效性验证类型Validation type 参数Arguments
xlValidateCustomxlValidateCustom Formula1 为必需,Formula2 遭忽略。Formula1 is required, Formula2 is ignored. Formula1 必须包含以下表达式:当数据输入有效时,表达式的计算结果为 True;否则,计算结果为 FalseFormula1 must contain an expression that evaluates to True when data entry is valid and False when data entry is invalid.
xlInputOnlyxlInputOnly
使用 *AlertStyle*、*Formula1*Formula2AlertStyle, Formula1, or Formula2 are used.
xlValidateListxlValidateList Formula1 为必需,Formula2 遭忽略。Formula1 is required, Formula2 is ignored. Formula1 必须包含以逗号分隔的值列表,或对该列表的工作表引用。Formula1 must contain either a comma-delimited list of values or a worksheet reference to this list.
xlValidateWholeNumberxlValidateDatexlValidateDecimalxlValidateTextLengthxlValidateTimexlValidateWholeNumber, xlValidateDate, xlValidateDecimal, xlValidateTextLength, or xlValidateTime 必须指定 Formula1Formula2 之一,或两者均指定。One of either Formula1 or Formula2 must be specified, or both may be specified.

示例Example

本示例向单元格 E5 添加数据有效性检验。This example adds data validation to cell E5.

With Range("e5").Validation 
 .Add Type:=xlValidateWholeNumber, _ 
 AlertStyle:= xlValidAlertStop, _ 
 Operator:=xlBetween, Formula1:="5", Formula2:="10" 
 .InputTitle = "Integers" 
 .ErrorTitle = "Integers" 
 .InputMessage = "Enter an integer from five to ten" 
 .ErrorMessage = "You must enter a number from five to ten" 
End With

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.