arrays - 查找字符串是否在二维 VBA Excel 数组中

标签 arrays vba excel

我有一个很棒的函数,我一直将其用于一维 Excel VBA 数组,用于检查字符串是否在数组中:

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean

    IsInArray = (UBound(Filter(arr(), stringToBeFound)) > -1)

End Function

不幸的是,当使用它来检查二维数组时它不起作用,就像我在这里一样:

Sub new_idea_filter()

home_sheet = ActiveSheet.Name

c = 1

Dim myfilters(1 To 4, 1 To 5000)


myfilters(1, 4) = "Test"

If IsInArray("Test", myfilters()) = True Then
    killer = True
End If



End Sub

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean

    IsInArray = (UBound(Filter(arr(), stringToBeFound)) > -1)

End Function

函数中不断出错,说下标超出范围,有人知道如何检查字符串是否在二维数组中吗?

最佳答案

我的代码集合中的一些内容

您可以使用Application.Match。这适用于 1D2D 数组:)

看这个

Sub Sample()
    Dim myfilters(1 To 4, 1 To 5000)

    myfilters(1, 4) = "Test"

    If IsInArray("Test", myfilters()) = True Then MsgBox "Found"
End Sub

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    Dim bDimen As Byte, i As Long

    On Error Resume Next
    If IsError(UBound(arr, 2)) Then bDimen = 1 Else bDimen = 2
    On Error GoTo 0

    Select Case bDimen
    Case 1
        On Error Resume Next
        IsInArray = Application.Match(stringToBeFound, arr, 0)
        On Error GoTo 0
    Case 2
        For i = 1 To UBound(arr, 2)
            On Error Resume Next
            IsInArray = Application.Match(stringToBeFound, Application.Index(arr, , i), 0)
            On Error GoTo 0
            If IsInArray = True Then Exit For
        Next
    End Select
End Function

关于arrays - 查找字符串是否在二维 VBA Excel 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30653135/

相关文章:

python - 在多维numpy数组中查找多个查询的索引

c++ - 为什么我不能将新对象添加到我的对象指针数组中?

arrays - 如何使用 swift 将新数据传输到数组?

VBA-替换字符串中的字符

vba - Excel 2003 - 如何构建我自己的 XLA?

Excel:如何使用通配符替换文本?

excel - 使用复选框通过 Onaction 传递参数

java - 多线程终止线程并授予其他线程访问权限

vba - 格式化(动态)

php - 如何为 laravel 5.4 安装 Maatwebsite/Laravel-Excel?