模仿“IN”操作员

发布于 2024-08-06 07:15:32 字数 185 浏览 6 评论 0原文

如何才能实现:

if X in (1,2,3) then

而不是:

if x=1 or x=2 or x=3 then

换句话说,如何才能最好地模仿 Excel 的 VBA 中的 IN 运算符?

How can one achieve:

if X in (1,2,3) then

instead of:

if x=1 or x=2 or x=3 then

In other words, how can one best imitate the IN operator in VBA for excel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

金橙橙 2024-08-13 07:15:32

我认为没有一个非常优雅的解决方案。

但是,您可以尝试:

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

I don't think there is a very elegant solution.

However, you could try:

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
泛泛之交 2024-08-13 07:15:32

您还可以尝试使用 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

You could also try the CASE statement instead of 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
寄离 2024-08-13 07:15:32

最快的方法:

这是一种比其他答案任何更快、更紧凑的方法,并且适用于数字或文本值:

Function IsIn(valCheck, valList As String) As Boolean  
    IsIn = Not InStr("," & valList & ",", "," & valCheck & ",") = 0
End Function

示例:

IsIn 与数值一起使用:

Sub demo_Number()
    Const x = 2
    If IsIn(x, "1,2,3") Then
        Debug.Print "Value " & x & " was Found!"
    Else
        Debug.Print "Value " & x & " was not Found."
    End If
End Sub

IsIn 与字符串值一起使用:

Sub demo_Text()
    Const x = "Dog"
    If IsIn(x, "Me,You,Dog,Boo") Then
        Debug.Print "Value " & x & " was Found!"
    Else
        Debug.Print "Value " & x & " was not Found."
    End If
End Sub

速度比较:

为了比较速度,我从接受的答案运行了测试 100,000 次:

我没有包含更长的答案< /a> 使用 SELECT..CASE ,因为与“if x=1 or x=2 or x=3 那么”。

Fastest Method:

Here's a method much faster and more compact than any of the other answers, and works with numeric or text values:

Function IsIn(valCheck, valList As String) As Boolean  
    IsIn = Not InStr("," & valList & ",", "," & valCheck & ",") = 0
End Function

Examples:

Use IsIn with a numeric value:

Sub demo_Number()
    Const x = 2
    If IsIn(x, "1,2,3") Then
        Debug.Print "Value " & x & " was Found!"
    Else
        Debug.Print "Value " & x & " was not Found."
    End If
End Sub

Use IsIn with a string value:

Sub demo_Text()
    Const x = "Dog"
    If IsIn(x, "Me,You,Dog,Boo") Then
        Debug.Print "Value " & x & " was Found!"
    Else
        Debug.Print "Value " & x & " was not Found."
    End If
End Sub

Speed Comparison:

To compare speed I ran the test from the accepted answer 100,000 times:

  • 0.406 sec (FASTEST) This Function (using InStr):
  • 1.828 sec (450% slower) Accepted Answer with the "ISIN" function
  • 1.799 sec (440% slower) Answer with the "IsInArray" from freeVBcode
  • 0.838 sec (206% slower) Answer with modified "IsInArray" function

I didn't include the much longer answer that uses SELECT..CASE since the OP's goal was presumably to simplify and shorten the task compared to "if x=1 or x=2 or x=3 then".

南街九尾狐 2024-08-13 07:15:32

你试过吗

eval("3 in(1,2,3,4,5)")

did you try

eval("3 in(1,2,3,4,5)")
紙鸢 2024-08-13 07:15:32

据我所知,没有一个。

我通常使用自制的 InArray() 函数,例如 http://www 中的函数.freevbcode.com/ShowCode.asp?ID=1675

您还可以制作一个迭代数组而不是连接的版本(如果这更适合您的数据类型)。

There's none that I'm aware of.

I usually use a home-brewed InArray() function like the one at http://www.freevbcode.com/ShowCode.asp?ID=1675

You could also make a version that iterates through the array instead of concatenating, if that is more appropriate to your data type.

对风讲故事 2024-08-13 07:15:32

如果不编写自己的函数,它就无法工作。请注意,@Kredns 接受的解决方案可能无法按预期适用于所有类型的对象,因为它们被强制为字符串(这也可能引发类型不匹配错误)。

该解决方案应该(希望)处理所有类型的数据(至少在 Excel 365 中,不确定早期版本):

Function IsIn(x As Variant, list As Variant) As Boolean
    ' Checks whether list (Array) contains the element x
    IsIn = False
    For Each element In list
        If x = element Then IsIn = True
    Next element
End Function

It doesn't work without writing your own function. Be aware that the accepted solution by @Kredns may not work as expected for all types of objects since they are coerced to strings (which also may raise Type Mismatch errors).

This solution should (hopefully) handle all types of data (at least in Excel 365, not sure about earlier versions):

Function IsIn(x As Variant, list As Variant) As Boolean
    ' Checks whether list (Array) contains the element x
    IsIn = False
    For Each element In list
        If x = element Then IsIn = True
    Next element
End Function
神魇的王 2024-08-13 07:15:32
dim x, y
x = 2
y = Array(1, 2, 3)

For i = 0 To 2
If x = y(i) Then
'your code comes here
Exit For
End If
Next i
dim x, y
x = 2
y = Array(1, 2, 3)

For i = 0 To 2
If x = y(i) Then
'your code comes here
Exit For
End If
Next i
夏末的微笑 2024-08-13 07:15:32

我现在写了...

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

I wrote it now...

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文