Results 1 to 4 of 4

Thread: Excel Table Range into Array and IsInArray function

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    3

    Excel Table Range into Array and IsInArray function

    Hello,

    I am a first time poster but a longtime admirer of the work and help provided on this forum.

    I have been banging my head on this and finally decided that I need to get some help. I am getting a type-mismatch error and I am not using Option Explicit. r = 19 so there are not many rows.
    Code:
    Sub TestArray()
    
    Dim r As Integer
    
    r = Range("ObjCode[[#All],[Cleaning]]").Rows.Count
    
    ReDim aFUNC(1 To r, 1 To 1) As Variant
    
    aFUNC = Range("ObjCode[[#All],[Cleaning]]")
    
    Debug.Print aFUNC(11, 1)
    
    If IsInArray(Range("G8").Value, aFUNC) Then
        MsgBox "Yeah!"
    End If
    
    
    End Sub
    
    Function IsInArray(stringToBeFound As Variant, arr As Variant) As Boolean
    
        IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
    
    End Function
    Any help is greatly appreciated

    Kles

  2. #2
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Excel Table Range into Array and IsInArray function

    Kles

    Welcome to the Forums
    What line of your code is throwing the error?

    Spoo

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    3

    Re: Excel Table Range into Array and IsInArray function

    The error occurs on:

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

    I think it is because arr is a one-dimensional array. I am not sure how to use the filter function on a 2-dimensional array. Thanks

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel Table Range into Array and IsInArray function

    from help
    Remarks

    If no matches of match are found within sourcearray, Filter returns an empty array. An error occurs if sourcearray is Null or is not a one-dimensional array.

    The array returned by the Filter function contains only enough elements to contain the number of matched items.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width