数据验证字符串中是否不允许使用括号?

时间:2019-02-20 16:48:23

标签: excel vba syntax

我正在通过VBA设置字段的数据验证。一切正常,直到将验证添加到字段为止,并且只在某些验证字符串上进行。似乎是字符串引起了问题。

我有以下数据验证代码:

If Not NewTarget Is Nothing Then
    With NewTarget.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:=mys
    End With

    NewTarget.Value = MyList(1)
End If

失败的mys的值是:

"Select One,Trade - Transportation - Utilities(9),Information,Finance -   Insurance - Real Estate,Professional - Business Services,Educational - Health Services,Leisure - Entertainment - Hospitality,Other Services (Except Public Administration),Public Administration"

有效的是:

"Select One,Natural Resources - Mining,Construction,Manufacturing"

是否需要从字符串中删除括号才能使验证字符串被接受?

我已经看到,使用VBA中的公式确实会引起字符串括号问题。

2 个答案:

答案 0 :(得分:2)

导致问题的原因不是括号,而是字符串的长度。

Excel中的公式限制为256个字符,当超出该限制时,Excel会引发异常。

答案 1 :(得分:0)

假设mys是一个范围,请执行以下操作:

.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:="=mys"