If IsInArray(Cells(r, 2), Break_List) = True Then

2019-08-22 03:38发布

I have been following this question quite deeply and trying to match Cell(r,2) on one sheet to an array I compiled below cycling through rows in column 2 on another sheet but I keep getting a False value returned from the function.

    Public Break_List(1 To 1000, 1 To 100) As Variant

    If IsInArray(Cells(r, 2), Break_List) = True Then

    Sub Store_Break_Categories()
    Sheets("BackEnd").Select
    Break_No_of_Rows = 0
    'For c = 10 To 15
    counter = 0
        If Cells(2, 3) <> "" Then
        lastrow = Cells(65000, 3).End(xlUp).Row
           For r = 2 To lastrow
              counter = counter + 1
               'Break_List(counter, c - 9) = Cells(r, c)
                  Break_List(counter, 1) = Cells(r, 3)
           Next r
        End If
        If counter > Break_No_of_Rows Then Break_No_of_Rows = counte
    End Sub

This is the function I have integrated from the above question

    Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
        IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))
    End Function

Thank you

1条回答
家丑人穷心不美
2楼-- · 2019-08-22 04:19

The Application.Match is not going to magically look through 100 columns. If you want to look through the first column then,

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    IsInArray = Not IsError(Application.Match(stringToBeFound, Application.Index(arr, 0, 1), 0))
End Function

If you want to look through all columns then,

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    dim a as long
    IsInArray = false
    for a = lbound(arr, 2) to ubound(arr, 2)
        If Not IsError(Application.Match(stringToBeFound, Application.Index(arr, 0, a), 0)) then
            IsInArray = true
            exit function
        end if
    next a
End Function
查看更多
登录 后发表回答