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

问题描述 投票:0回答:1

我有以下代码:

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。我怎样才能做到这一点?

excel-vba validation vba excel
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.