VBA如何检查变量是否在数组中表示

时间:2016-01-01 17:57:27

标签: arrays vba

我有InputBox,用户可以在文本字段中输入任何值。我想检查这个值是否在数组中表示:例如,“asdf”不在数组ValidEntries中,它包含整数0,1,2,3,4,5和6,但是“6”是。这是怎么回事?

2 个答案:

答案 0 :(得分:3)

以下是三种可能的解决方案:

Sub test1()
    Dim ValidEntries As Variant
    Dim i As Long, v As Variant
    Dim valid As Boolean

    ValidEntries = Array(1, 2, 3, 4, 5, 6)
    v = InputBox("Enter something")

    valid = False
    For i = LBound(ValidEntries) To UBound(ValidEntries)
        If v = ValidEntries(i) Then
            valid = True
            Exit For
        End If
    Next i

    MsgBox v & IIf(valid, " is valid", " isn't valid")

End Sub

Sub test2()
    Dim ValidEntries As Variant
    Dim v As Variant
    Dim valid As Boolean

    ValidEntries = Array(1, 2, 3, 4, 5, 6)
    v = InputBox("Enter something")

    valid = Join(ValidEntries, "@") Like "*" & v & "*" And Not (v Like "*@*")

    MsgBox v & IIf(valid, " is valid", " isn't valid")

End Sub

Sub test3()
    Dim ValidEntries As Variant
    Dim i As Long, v As Variant, key As Variant
    Dim valid As Boolean
    Dim dict As Object

    Set dict = CreateObject("Scripting.Dictionary")

    ValidEntries = Array(1, 2, 3, 4, 5, 6)
    For i = LBound(ValidEntries) To UBound(ValidEntries)
        key = Trim(Str(ValidEntries(i)))
        If Not dict.exists(key) Then dict.Add key, 0
    Next i
    v = InputBox("Enter something")

    valid = dict.exists(v)

    MsgBox v & IIf(valid, " is valid", " isn't valid")

End Sub

第一个是对数组进行简单的线性搜索。第二个采用分隔符,该分隔符不能出现在任何有效条目中(您可能必须选择另一个),该分隔符用于将数组连接成一个大字符串,然后检查输入是否为子字符串(但不是跨越分隔符的一个)。第三个创建一个字典,之后您可以在O(1)时间内检查成员资格。为了验证单个输入,这可能不值得,但如果您的代码必须反复检查数组中的成员资格,那么它可能是值得的。

答案 1 :(得分:1)

如果您正在尝试查看数组中是否存在整数的字符串表示形式,只需使用CInt()

将字符串转换为整数
Sub Foo()

Dim Bar As Variant

Bar = InputBox("Enter a number:")

If IsNumeric(Bar) Then    '// Check if the entry represents a number
    Bar = CInt(Bar)       '// If it is then cast "Bar" to an Integer
Else
    MsgBox Bar & " Is not a valid number"
    Exit Sub              '// If not then exit the sub routine
End If

'// I've used Evaluate() as an example, you would use whatever method you like for checking the presence in the array.
If Evaluate("ISERROR(MATCH(" & Bar & ",{0,1,2,3,4,5,6},0))") Then
    MsgBox Bar & " does not exist in array."
Else
    MsgBox Bar & " exists in array."
End If

End Sub

但是,如果您要求如何检查是否存在数组条目,则可以尝试以下3种方法:

从数组中创建一个字符串并使用Evaluate():

Sub MM_1()
    Const x As Integer = 5 '// Change to any number to test
    y = Array(1, 2, 3, 4, 5, 6)

    If Evaluate("ISERROR(MATCH(" & x & ",{" & Join(y, ",") & "},0))") Then
        MsgBox x & " does NOT exist in array"
    Else
        MsgBox x & " DOES exist in array"
    End If
End Sub  

或者使用ArrayList对象:

Sub MM_2()

Const x As Integer = 5 '// Change to any number to test
Set y = CreateObject("System.Collections.ArrayList")

For i = 1 To 6
    y.Add i
Next

If y.Contains(x) Then
    MsgBox x & " DOES exist in the array."
Else
    MsgBox x & " does NOT exist in the array."
End If

End Sub

有关设置值的列表,请使用Select Case语句而不是数组:

Sub MM_3()

Const x As Integer = 5 '// Change to any number to test

Select Case x
    Case 1, 2, 3, 4, 5, 6
        MsgBox x " DOES exist in the array."
    Case Else
        MsgBox x " does NOT exist in the array."
End Select

End Sub