+ Reply to Thread
Results 1 to 3 of 3

Matching two arrays - Part 1

Hybrid View

PradeepRed Matching two arrays - Part 1 09-17-2010, 03:48 AM
DonkeyOte Re: Matching two arrays -... 09-17-2010, 03:57 AM
DonkeyOte Re: Matching two arrays -... 09-17-2010, 04:16 AM
  1. #1
    Registered User
    Join Date
    12-14-2005
    MS-Off Ver
    2016
    Posts
    12

    Matching two arrays - Part 1

    Hi

    Need some help.
    This is what I am facing
    I have two arrays with the following unique values
    Array1: a,b,c,d,e (all unique,no repetition)
    Array2: a,b,c,d,e,f,g,h,i,j (these values can be repeated as a,b,a,a,c,b,j,h...)

    Every time the array 2 element matches the array 1 element, in a new column I want the value 1, else 0
    eg for the above series (1,1,1,1,1,1,0,0)

    I have looked up the forum but not found the right answer.
    Please help.

    Thanks in advance

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Matching two arrays - Part 1

    I have no doubt the other guys who are actually good with Arrays can come up with a more efficient method than this

        Dim v1 As Variant, v2 As Variant
        Dim b1() As Byte
        Dim lngX As Long
        v1 = Array("a", "b", "c", "d", "e")
        v2 = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
        ReDim b1(LBound(v2) To UBound(v2))
        For lngX = LBound(v1) To UBound(v1) Step 1
            b1(lngX) = -(v1(lngX) = v2(lngX))
        Next lngX
    depending on case sensitivity of match etc you might wish to adjust the comparison method

    edit:

    Quote Originally Posted by PradeepRed
    Every time the array 2 element matches the array 1 element
    do you mean you want to conduct a Match on each element to the unique vals to see if it exists (irrespective of position) - I suspect you do ....
    Last edited by DonkeyOte; 09-17-2010 at 04:08 AM.

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Matching two arrays - Part 1

    If my edit's correct then in theory you could use something like the below rather than iterating v2 item by item:

        Dim v1, v2, v3
        v1 = Array("a", "b", "c", "d", "e")
        v2 = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
        v3 = Application.IsNumber(Application.Match(v2, v1, 0))
    though the above in that form would return Boolean True/False rather than 1/0
    (and Match is not case sensitive - unknown if that's an issue or not)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1