查找一个string是否在二维VBA Excel数组中

我有一个伟大的function,我所有的时间用于检查一个string是否在一个数组中的一维Excel VBA数组:

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean IsInArray = (UBound(Filter(arr(), stringToBeFound)) > -1) End Function 

不幸的是,它使用它来检查一个2维数组,如我在这里不工作:

 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 

它不断的在下标超出范围的函数中出错,任何人都有一个想法,我可以检查一个string是否在二维数组?

来自我的代码收集

您可以使用Application.Match 。 这将适用于1D2Darrays:)

看到这个

 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 

只要你在Excel中(或者有一个对它的引用),你可以使用Index函数将你的数组切分成行或列。

 Public Function IsInArray(ByVal vToFind As Variant, vArr As Variant) As Boolean Dim i As Long Dim bReturn As Boolean Dim vLine As Variant For i = LBound(vArr, 1) To UBound(vArr, 1) vLine = Application.WorksheetFunction.Index(vArr, i) 'slice off one line If IsArray(vLine) Then 'if it's an array, use the filter bReturn = UBound(Filter(vLine, vToFind)) > -1 Else 'if it's not an array, it was 1d so check the value bReturn = vLine = vToFind End If If bReturn Then Exit For 'stop looking if one found Next i IsInArray = bReturn End Function Public Sub test() Dim arr() As Variant ReDim arr(1 To 2, 1 To 2) arr(1, 2) = "Test" Debug.Assert IsInArray("Test", arr) arr(1, 2) = "Wrong" Debug.Assert Not IsInArray("Test", arr) ReDim arr(1 To 3) arr(2) = "Test" Debug.Assert IsInArray("Test", arr) arr(2) = "Wrong" Debug.Assert Not IsInArray("Test", arr) Debug.Print "Passed" End Sub 

如果您从logging集中获取数据,则使用此方法; 首先我使用GetString的logging集,第二次使用拆分转换数组中的string单维度,其中每个项目是一个string与所有的信息。 之后,你使用函数IsInArray。

代码是:

 RecSet.Open strSQL, Cn RecSet.MoveFirst RecString = RecSet.GetString(, , ";", vbCr) 'genera una cadena con los datos. Campos separados por ; y registros por vbCR RecSplit = Split(RecString, vbCr) 'Genera un array unidimensional con la cadena 

您可以testing代码,但只记得从logging集中获取数据时才有效

您可以尝试转换您的原始函数,以便能够使用数组。 请尝试以下,但请注意,我还没有testing,如果它的工作。

 Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean Dim cell As Variant For Each cell In arr IsInArray = IsInArray Or (UBound(Filter(cell(), stringToBeFound)) > -1) Next End Function 

问候

上面的@ Siddharth-Rout的答案与Application.Match除了Filter函数完全一样:-)。 – 我的解决scheme只尝试使用OP Filter函数:由于过滤函数需要1dim数组,所以数组被分割成几部分。

A)使用原始FILTER函数替代Match +error handling的替代解决scheme

 Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean Dim i As Long If nDim(arr) = 1 Then IsInArray = (UBound(Filter(arr(), stringToBeFound)) > -1) Else ' allows using filter function in portions For i = 1 To UBound(arr, 2) If (UBound(Filter(Application.Transpose(Application.Index(arr, 0, i)), stringToBeFound)) > -1) Then IsInArray = True: Exit For Next i End If End Function 

Helper函数获取数组Dimension

 Function nDim(ByVal vArray As Variant) As Long ' Purp: get number of array dimensions ' Site: http://support.microsoft.com/kb/152288 Dim dimnum As Long Dim ErrorCheck As Variant On Error GoTo FinalDimension For dimnum = 1 To 60000 ErrorCheck = LBound(vArray, dimnum) Next FinalDimension: nDim = dimnum - 1 End Function 

B)recursion解决scheme使用原始的FILTER函数而不是Match +error handling

 Function IsInArray(stringToBeFound As String, arr As Variant, Optional i As Long = 0) As Boolean Select Case i Case -1: ' stop 2dim calls Case 0: IsInArray = IsInArray(stringToBeFound, arr, nDim(arr)) ' start recursive call Case 1: IsInArray = (UBound(Filter(arr(), stringToBeFound)) > -1) ' 1dim array Case Else ' allows using filter function in portions If (UBound(Filter(Application.Transpose(Application.Index(arr, 0, i)), stringToBeFound)) > -1) Then IsInArray = True Else ' recursive calls (2dim array) IsInArray = IsInArray(stringToBeFound, arr, IIf(i + 1 > UBound(arr), -1, i + 1)) End If End Select End Function