数据验证,只有2个选项(所以或)

时间:2018-03-06 12:14:19

标签: excel-vba validation vba excel

我有以下代码:

If StrSoort = "Tussen_Een_en_Vijf" Then   bKolomHidden = "False"   Range(StrBereik).Select
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateDecimal, AlertStyle:=xlValidAlertStop, Operator _
        :=xlBetween, Formula1:="1", Formula2:="5"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = "Only values between 1 and 5"
        .ErrorMessage = "Only values between 1 and 5"
        .ShowInput = True
        .ShowError = True
    End With
    Selection.NumberFormat = "0.00"

检查输入值是否介于1和5之间。 相反,我想更改此设置以验证1或5.但是我找不到or / XlOr。 我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

我认为必须单独添加它们,因为需要单元格地址。

dim dv as range
for each dv in Selection
  with dv.Validation
    .Delete
    .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=or(" & dv.address(0, 0) & "=1," & dv.address(0, 0) & "=5)"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = "only 1 or 5"
    .ErrorMessage = "only 1 or 5"
    .ShowInput = True
    .ShowError = True
  End With
next dv