使用 ValidationType.List 将输入限制为“x,X”
您可以使用ValidationType.List 枚举选项来完成此操作。示例:
worksheet.Range(SomeRange).Validation.Add(ValidationType.List, ValidationAlertStyle.Stop, ValidationOperator.Default, "x,X", Nothing)
不使用 ValidationType.List 将输入限制为“x,X”
当单元格被选中时,ValidationType.List 枚举选项将始终显示一个下拉列表。如果您不希望出现此下拉菜单,则需要采用另一种方法,提供自定义公式来进行验证 (ValidationType.Custom),这可能需要一些努力才能正常工作,因为您是负责验证SomeRange中每个单元格的输入。
以下是解决此问题的一般方法(请注意,我使用的实际公式只是完成任务的一种方法......任何数量的其他公式都可以用来做同样的事情):
' Get IRange representing your SomeRange string (say, "A1:B5").
Dim MyRange As IRange = worksheet.Range(SomeRange)
' Get (relative-referenced) address of top-left cell for SomeRange (A1 in this case).
' We need to use this cell address in the formula to validate input. This approach
' will still work fine if "SomeRange" is a single cell instead of a multi-cell reference.
Dim TopLeftCell As String = MyRange(0, 0).GetAddress(False, False,
ReferenceStyle.A1.A1, False, Nothing)
' Put together a "validation formula" (i.e., =LOWER(A1)="x").
Dim ValidationFormula = String.Format("=LOWER({0})=""x""", TopLeftCell)
' Create validation for all cells in MyRange (A1:B5 in this example), using the
' ValidationFormula as the basis for this validation.
MyRange.Validation.Add(ValidationType.Custom, ValidationAlertStyle.Stop,
ValidationOperator.Default, ValidationFormula, Nothing)