我正在编写一个需要输入的函数,我的数据验证看起来很尴尬。如果InputFld不是“A”,“B”或“C”,则表示错误:
If InputFld <>"A" and InputFld<>"B" and InputFld<>"C" then goto ErrorHandler
这对我来说太丑了。有更优雅的解决方案吗?我想写一些类似的东西:
If InputFld not in ("A","B","C") then goto ErrorHandler
请参阅?这种方式更容易阅读和维护。但我不知道该怎么做。
答案 0 :(得分:5)
怎么样:
If Instr("ABC",InputFld)=0 Then
答案 1 :(得分:2)
至少有两种方法可以做到:
public function IsInSet(byval value as variant, paramarray theset() as variant) as boolean
dim i as long
for i=lbound(theset) to ubound(theset)
if value = theset(i) then
isinset = true
exit function
end if
next
end function
用法:If not IsInSet(val, "A", "B", "C") then ...
public function IsInSet(byval value as variant, theset as variant) as boolean
dim i as long
for i=lbound(theset) to ubound(theset)
if value = theset(i) then
isinset = true
exit function
end if
next
end function
用法:If not IsInSet(val, array("A", "B", "C")) then ...
答案 2 :(得分:1)
Eval()应该允许你做类似的事情。该表达式返回-1(True):
Debug.Print Eval("""g"" Not In (""a"",""b"",""c"")")
但是,我不会称之为优雅。
考虑使用Like运算符。该表达式返回True:
Debug.Print Not "g" Like "[abc]"