Consulting

Results 1 to 7 of 7

Thread: Sleeper: Comparing arrays

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Sleeper: Comparing arrays

    I'm looking for a list of items from Data1, not contained in Data2. Any other methodologies simpler/better than this?

    Dim Test1, Test2, Result(20)
        Test1 = Range("Data1").Value
        Test2 = Range("Data2").Value
        For Each x In Test1
            pos = 0
            On Error Resume Next
            pos = Application.WorksheetFunction.Match(x, Test2, 0)
            If pos = 0 Then
                i = i + 1
                Result(i) = x
            End If
        Next
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Lookin pretty good to me. Anything specific you want to do to it?




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mdmackillop
    I'm looking for a list of items from Data1, not contained in Data2. Any other methodologies simpler/better than this?

    Dim Test1, Test2, Result(20)
        Test1 = Range("Data1").Value
        Test2 = Range("Data2").Value
        For Each x In Test1
            pos = 0
            On Error Resume Next
            pos = Application.WorksheetFunction.Match(x, Test2, 0)
            If pos = 0 Then
                i = i + 1
                Result(i) = x
            End If
        Next
    Couple of things. By using Application.MATCH, and by virtue of the fact that you don't re-use the variable pos, you can dispense with it altogether.

    You also don't populate item 0 of the array, so best not declare it.

    Finally, you leave Resume Next on, which you shouldn't, but by just using Application.MATCH you don't need it anyway.


    Dim Test1, Test2, Result(1 To 20)
    Dim x
    Dim i As Long
        Test1 = Range("Data1").Value
        Test2 = Range("Data2").Value
        For Each x In Test1
            If IsError(Application.Match(x, Test2, 0)) Then
                i = i + 1
                Result(i) = x
            End If
        Next
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by xld
    ...
    You also don't populate item 0 of the array, so best not declare it.
    why?
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by MWE
    why?
    TRy this and see if you can work it out yourself


    Dim Test1, Test2, Result(20)
    Dim x
    Dim i As Long
    Test1 = Range("Data1").Value
    Test2 = Range("Data2").Value
    For Each x In Test1
        If IsError(Application.Match(x, Test2, 0)) Then
            i = i + 1
            Result(i) = x
        End If
    Next
    For i = LBound(Result) To UBound(Result)
        If Result(i) = "" Then
            If i = LBound(Result) Then
                MsgBox "array is empty"
                Exit For
            End If
        End If
    Next i
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    It is good practice to explicitly declare everything. Nothing shows this better than jumping to the conclusion that an unspecified lbound is 0.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks all,
    Good suggestions Bob, I'll make the changes in my code.
    Regards
    Malcolm
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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