模仿“IN”操作者 [英] Imitating the "IN" Operator
本文介绍了模仿“IN”操作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何实现:
if X in (1,2,3) then
而不是:
if x=1 or x=2 or x=3 then
换句话说,最好的模仿VBA for excel中的 IN
运算符
In other words, how can one best imitate the IN
operator in VBA for excel?
推荐答案
我不认为有一个非常优雅的解决方案。
I don't think there is a very elegant solution.
但是,您可以尝试:
If Not IsError(Application.Match(x, Array("Me", "You", "Dog", "Boo"), False)) Then
或者您可以编写自己的功能:
or you could write your own function:
Function ISIN(x, StringSetElementsAsArray)
ISIN = InStr(1, Join(StringSetElementsAsArray, Chr(0)), _
x, vbTextCompare) > 0
End Function
Sub testIt()
Dim x As String
x = "Dog"
MsgBox ISIN(x, Array("Me", "You", "Dog", "Boo"))
End Sub
这篇关于模仿“IN”操作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文