【VBA】[2次元配列] 検索する

'■二次元配列内の要素を検索し、条件に一致した場所を探す
Public Function Call_ArraySearch2D(arr As Variant, sWord As String)
    Dim i As Long, j As Long, k As Long
    Dim tmp As Variant
    ReDim tmp(UBound(arr, 1), UBound(arr, 2))
    r = LBound(arr, 1)
    c = LBound(arr, 2)
     
    '■単純にFor~Loopで回して検索する
    For i = LBound(arr, 1) To UBound(arr, 1)
        If arr(i, LBound(arr, 2)) = sWord Then
            For j = LBound(arr, 2) To UBound(arr, 2)
                tmp(r, c) = arr(i, j)
                c = c + 1
            Next j
            r = r + 1
            c = LBound(arr, 2)
        End If
    Next i
     
    Call_ArraySearch2D = Call_RedimPreserveArray(tmp, r - 1)
End Function

コメントする