在数组中查找字符串时出现类型不匹配

Getting type mismatch when looking up string in array

我有一些代码可以从电子表格中提取字符串并在数组中查找字符串的索引值。我注意到如果我将字符串保留为 1 个字符,代码就可以正常工作。如果我真的尝试 运行 它查找单词,我会收到类型不匹配错误,即使字符串确实存在于数组中。任何帮助,将不胜感激。我已经粘贴了下面代码的简化版本,它仍然会导致相同的错误。

我试过将我的数组变暗为变体或字符串。我尝试使用 white-space 使数组中的所有字符串长度相同。

Sub myArray_ISbroke()

    Dim arraysSuck: arraysSuck = Split("HI,HELLO,TEST1,TEST2,T3", ",")
    MsgBox Application.Match("HI", arraysSuck)
    MsgBox Application.Match("HELLO", arraysSuck)
    MsgBox Application.Match("TEST1", arraysSuck)
    MsgBox Application.Match("TEST2", arraysSuck)
    MsgBox Application.Match("T3", arraysSuck)

End Sub

我希望 MSG 框显示 1、2、3、4 和 5。我在“1”后收到错误 13。

您缺少最后一个参数,即匹配类型。请参阅文档 here.

它说:

If match_type is 1, Match finds the largest value that is less than or equal to lookup_value. Lookup_array must be placed in ascending order: ...-2, -1, 0, 1, 2, ..., A-Z, FALSE, TRUE.

If match_type is 0, Match finds the first value that is exactly equal to lookup_value. Lookup_array can be in any order. Note that Match is case-insensitive.

If match_type is -1, Match finds the smallest value that is greater than or equal to lookup_value. Lookup_array must be placed in descending order: TRUE, FALSE, Z-A, ...2, 1, 0, -1, -2, ..., and so on.

If match_type is omitted, it is assumed to be 1.

修改后的代码:

Option Explicit

Sub UpdatedExample()

    Dim arraysSuck: arraysSuck = Split("HI,HELLO,TEST1,TEST2,T3", ",")
    MsgBox Application.Match("HI", arraysSuck, 0)
    MsgBox Application.Match("HELLO", arraysSuck, 0)
    MsgBox Application.Match("TEST1", arraysSuck, 0)
    MsgBox Application.Match("TEST2", arraysSuck, 0)
    MsgBox Application.Match("T3", arraysSuck, 0)

End Sub