我正在通过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中的公式确实会导致字符串中的括号出现问题。
事实证明,它不是引起问题的括号,而是字符串的长度。
Excel中的公式具有256个字符的限制,当超出该限制时,Excel会引发异常。
假设mys
是一个范围,请执行以下操作:
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="=mys"