2016-12-28 31 views
2

我想設計一個Function或Sub,它接受任意數量的布爾條件並將它們添加到IF語句中。我想象中的代碼是這樣的:要在IF語句中使用的VBA參數陣列「條件」

Function comp (ParamArray list() As Variant) 
    If (list(0) = True) And (list(1) = true) ..... (list(last) = true) then 
'random code 
    End If 
End Function 

其中的list()會像expresions:

x = y-1,somethingToCompare <> anotherThing, etc... 

這將是有趣的,如果我可以添加「和」爲另一種說法,要改變到「或」,如果我想。

Function comp (VyVal compare as Boolean, ParamArray list() As Variant) 

dim comparison as String??? 

    If compare then 
    comparison = "and" 
    else 
    comparison = "or" 
    end if 

    If (list(0) = True) comparison (list(1) = true) ..... (list(last) = true) then 

    'random code 

    End If 
End Function 

最終的想法是使用像這樣的功能:

Sub code() 

if comp(True, condition1, condition2, ...) then 
'random code' 

End Sub 

避免直接看代碼我寫,以免燒傷你的眼睛。

是這樣posible還是應該我得到一個棒棒糖?

也許我是以錯誤的方式來看待這個問題,並且有一種更簡單的方法可以做類似甚至更好的事情。

回答

1

的Python(和一些其他語言)具有實用功能all()any()其作爲輸入數組(或其他一些可迭代的),並根據是否有一些,全部或者沒有通過的布爾值爲真來返回True或False。你可以寫的這些VBA版本(使用Some()代替Any()因爲Any恰好是在VBA一個有點模糊關鍵字):

Function All(ParamArray conditions()) As Boolean 
    Dim i As Long 
    For i = LBound(conditions) To UBound(conditions) 
     If Not conditions(i) Then 
      All = False 
      Exit Function 
     End If 
    Next i 
    All = True 
End Function 

Function Some(ParamArray conditions()) As Boolean 
    Dim i As Long 
    For i = LBound(conditions) To UBound(conditions) 
     If conditions(i) Then 
      Some = True 
      Exit Function 
     End If 
    Next i 
    Some = False 
End Function 

你可以直接使用這些功能來有條件地代碼。

可以說,它可能是更有效改變上述定義到:

Function All(conditions As Variant) As Boolean 
    Dim i As Long 
    For i = LBound(conditions) To UBound(conditions) 
     If Not conditions(i) Then 
      All = False 
      Exit Function 
     End If 
    Next i 
    All = True 
End Function 

Function Some(conditions As Variant) As Boolean 
    Dim i As Long 
    For i = LBound(conditions) To UBound(conditions) 
     If conditions(i) Then 
      Some = True 
      Exit Function 
     End If 
    Next i 
    Some = False 
End Function 

現在你需要,如果你有條件的文字列表中使用電話一樣Some(Array(c1, c2, c3))而非Some(c1,c2,c3),但你將有靈活地通過一系列的條件。使用這個第二個定義,你可以寫類似下面的(這回答了你原來的問題):然後,例如,正在打印

Sub Sometimes(when As String, ParamArray conditions()) 
    Dim run As Boolean 
    Dim cond As Variant 

    cond = conditions 
    run = IIf(when = "All", All(cond), Some(cond)) 
    If run Then 
     Debug.Print "Running random code" 
    Else 
     Debug.Print "Not running random code" 
    End If   
End Sub 

Sometimes "Some",True,True,False結果Running random code

1
sub pointless(byval IsAnd as boolean, paramarray conditions()) 
    dim i as long 

    for i=lbound(conditions) to ubound(conditions) 
    if IsAnd then 
     if not conditions(i) then exit sub 
    else 
     if conditions(i) then exit for 
    end if 
    next 

    'random code 
end sub 

但你應該明白,程序將收到結果傳遞給它的比較,而不是比較自己的。因此,有沒有一點真正擺在首位,以有這樣的過程中,你可以直接寫在代碼:

if x = y-1 and somethingToCompare <> anotherThing then 
    'random code 
end if