代码之家  ›  专栏  ›  技术社区  ›  PowerUser

VBA中有NotIn(“a”,“B”)函数吗?

  •  1
  • PowerUser  · 技术社区  · 14 年前

    If InputFld <>"A" and InputFld<>"B" and InputFld<>"C" then goto ErrorHandler

    我觉得这很难看。有没有更优雅的解决方案?我只想写下这样的话:

    If InputFld not in ("A","B","C") then goto ErrorHandler

    看到了吗?这样更容易阅读和维护。但我不知道怎么做。

    3 回复  |  直到 14 年前
        1
  •  2
  •   GSerg    14 年前

    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
  •  5
  •   Fionnuala    14 年前

    怎么样:

    If Instr("ABC",InputFld)=0 Then
    
        3
  •  1
  •   HansUp    14 年前

    Eval()应该允许您执行类似的操作。此表达式返回-1(True):

    Debug.Print Eval("""g"" Not In (""a"",""b"",""c"")")
    

    不过,我不认为这很优雅。

    考虑使用Like操作符。此表达式返回True:

    Debug.Print Not "g" Like "[abc]"