是否有一个NotIn(" A"," B")在VBA功能?有一个、功能、QUOT、NotIn

2023-09-08 11:24:49 作者:劳资、何曾输过

我正在写一个函数,需要输入我的数据验证看起来很别扭。如果InputFld不是A,B或C,那么这是一个错误:

I'm writing a function that requires input and my data validation looks very awkward. If InputFld isn't "A","B", or "C", then it's an error:

如果InputFld<>A和InputFld<>B和InputFld<>C,然后转到的ErrorHandler

这只是看起来丑陋的我。有没有更好的解决方案?我想只写是这样的:

This just looks ugly to me. Is there a more elegant solution? I'd like to just write something like:

如果InputFld不在(A,B,C),然后转到的ErrorHandler

请参阅?更容易阅读和维护这种方式。但我不知道该怎么做。

See? Much easier to read and maintain this way. But I don't know how to do it.

推荐答案

目前至少有两种方法可以做到这一点:

At least two ways to do that:

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

用法:如果没有IsInSet(VAL,A,B,C),那么...

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

用法:如果没有IsInSet(VAL,阵列(A,B,C)),那么...