如何才能实现:代替模仿 “IN” 操作
if X in (1,2,3) then
:
if x=1 or x=2 or x=3 then
换句话说,怎样才能最好的一个模仿IN
运营商VBA为Excel?
如何才能实现:代替模仿 “IN” 操作
if X in (1,2,3) then
:
if x=1 or x=2 or x=3 then
换句话说,怎样才能最好的一个模仿IN
运营商VBA为Excel?
我不认为有一个非常优雅的解决方案。
但是,你可以尝试:
If Not IsError(Application.Match(x, Array("Me", "You", "Dog", "Boo"), False)) Then
,或者你可以写自己的函数:
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
你尝试
eval("3 in(1,2,3,4,5)")
这应该工作。 –
我得到“子或功能未定义” - 不知道这是因为我在VBA(不是VB)?已编辑该问题... –
它是Access VBA功能。对不起,我没有看到你提到的Excel VBA。 – THEn
有没有,我所知道的。
我通常使用像一个自酿InArray()函数在http://www.freevbcode.com/ShowCode.asp?ID=1675
你也可以做一个版本,通过数组,而不是串联迭代,如果是更适合您的数据类型。
你也可以尝试CASE语句而不是IF
Select Case X
Case 1 To 3
' Code to do something
Case 4, 5, 6
' Code to do something
Case 7
' Code to do something
Case Else
' More code or do nothing
End Select
您也可以作为描述here
使用过滤功能我现在写的......
Public Function IsInArray(FindValue As Variant, ParamArray arrEmailAttachment()) As Boolean
Dim element As Variant
For Each element In arrEmailAttachment
If element = FindValue Then
IsInArray = True
Exit Function
End If
Next element
IsInArray = False
End Function
难道你不能只是避免功能,并使用类似: 'InStr( 1, “MeYouDogBoo”,x)的'? – redOctober13
@ redOctober13如果使用在x中找不到的分隔符分隔列表项,这可能会起作用。否则,YouDo会触发InStr。 – mrdaddychops