+ Reply to Thread
Results 1 to 4 of 4

Thread: Compare 2 arrays, results into third array

  1. #1
    Registered User
    Join Date
    09-27-2010
    Location
    Adelaide
    MS-Off Ver
    Excel 2007
    Posts
    5

    Exclamation Compare 2 arrays, results into third array

    Hi all,

    I have two arrays i need to do an element comparison on.
    Arr is the first array, which is a list from a worksheet range.
    ArrRestults is the return results from an sql query, using Arr in the where clause.

    ArrResults will contain the same or less data/elements than Arr.

    Is there a simple way to loop through Arr, and find any element/data that isn't in ArrResults, and if found, put that value into a third array? (i.e. ArrFound)

    Cheers,
    Skmr3.

  2. #2
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,151

    Re: Compare 2 arrays, results into third array

    AHi skimmer333

    welcome to the forum
    one possible way would be with a Dictionary
          With CreateObject("Scripting.Dictionary")
               .comparemode = vbTextCompare
            For i = 1 To UBound(myarray, 1)
                If Not .Exists(myarray(i, 1)) Then
                    n = n + 1
                     .Add myarray(i, 1), n
                End If
             Next
            For i = 1 To UBound(myarrayTwo, 1)
                If Not .Exists(myarrayTwo(i, 1)) Then
                    n = n + 1
                     .Add myarrayTwo(i, 1), n
                Else
                 MsgBox myarrayTwo(i, 1) & " Exists"
                End If
             Next
        End With
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  3. #3
    Registered User
    Join Date
    09-27-2010
    Location
    Adelaide
    MS-Off Ver
    Excel 2007
    Posts
    5

    Thumbs up Re: Compare 2 arrays, results into third array

    Quote Originally Posted by pike View Post
    AHi skimmer333

    welcome to the forum
    one possible way would be with a Dictionary
    Thanks for your reply pike.

    Your code got me thinking again, and I got this to work:
    For Each c In Arr()
        For i = LBound(ArrResults) To UBound(ArrResults)
        If c = ArrResults(i) Then
                ActiveCell.Value = c
                ActiveCell.Offset(1, 0).Activate
        End If
        Next
    Next c
    It dumps out all values that are in Arr and also in ArrResults.
    Which looks correct.

    Now, i'm just trying to work out the reverse.
    Dump out all values that are in Arr but not in ArrResults.

    /Skmr3.

  4. #4
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,151

    Re: Compare 2 arrays, results into third array

    Hi skimmer333,
    yep, it will work fine. May be a little slow with large data.Dict are quicker.
    Another way would be to compare the arrays as they are created
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0