我想設計一個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還是應該我得到一個棒棒糖?
也許我是以錯誤的方式來看待這個問題,並且有一種更簡單的方法可以做類似甚至更好的事情。