+ Reply to Thread
Results 1 to 11 of 11

Intersection of two sets of values?

Hybrid View

  1. #1
    Registered User
    Join Date
    02-07-2012
    Location
    NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    42

    Intersection of two sets of values?

    Dear Excel Gurus,
    Is there a way to isolate an intersection of two sets of values?

    Suppose in A1:A5 I have the following five values: a, a, b, c, d. In cells B1:B6 I have a, f, a, f, c, f. I want to find an intersection of the two sets and put them in column C. In other words, I want to have a in C1 and c in C2 since a and c are the only two values that occur in both sets of values (A1:A5 and B1:B6).

    I suppose I can create an indicator column and use vlookup with exact match to figure out which of the values that occur in A1:A5 also occur in B1: B6. But I am wondering if there is an easier way to do it: something like a pre-rpogrammed function or a button similar to "Remove duplicates"?


    Thank you very much.

    Studiosa

  2. #2
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Intersection of two sets of values?

    could you include a sample set of data with desired end results please?
    If you liked my solution, please click on the Star -- to add to my reputation

    If your issue as been resolved, please clearly state so and mark the thread as [SOLVED] using the thread tools just above the first post.

  3. #3
    Registered User
    Join Date
    02-07-2012
    Location
    NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Intersection of two sets of values?

    Hi,
    Yes, here is an example (attached). There are two sets of text strings. I would like to get a third set that contains one instance of every string that occurs at least once in both input sets.

    I can figure out how to do that by first copying each of the two sets and pasting them into the same column, then eliminating duplicates. This would result in a "union" of the two sets.
    Then adjacent to the union of the two sets I can have an "indicator" column that would use a couple of vlookup commands to determine whehter each text string in the union set occurs in both of the initial sets.

    Then I can select those items from the union set that occur in both input sets. That would be the intersection.

    I am wondering if there is a simpler way to accomplish this. (I do not know Visual Basic at this moment)


    Many thanks.
    Studiosa
    Attached Files Attached Files

  4. #4
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Intersection of two sets of values?

    Give this macro a shot, it should work on your layout

    Sub findmatches()
    Dim r As Integer
    r = 4
    For i = 4 To Range("B10000").End(xlUp).Row
        If WorksheetFunction.CountIf(Range("C:C"), Range("B" & i).Value) > 0 Then
            If WorksheetFunction.CountIf(Range("D:D"), Range("B" & i).Value) = 0 Then
                Range("D" & r).Value = Range("B" & i)
                r = r + 1
            End If
        End If
    Next i
    
    End Sub

  5. #5
    Registered User
    Join Date
    02-07-2012
    Location
    NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Intersection of two sets of values?

    Hi,
    Thank you very much for your suggestion. I tried it and it works! (After I figured out how to input VBA!)

    I have not used VBA or macros before. I am wondering if you would be kind enough to explain something in your code:

    What exactly does this do:
    Range("B10000").End(xlUp).Row
    I think it somehow figures out what is the row number is for the last (biggest row number) non-empty row in column B. But I do not know enough to figure out how it does this.
    I understood that Range("B10000") specifies cell B10000 (so I guess there is a built in assumption that I will have no more than 10000 rows of data in column B, right?) But I am confused about the End(xlUp).Row I read the help on VBA, but still did not get it.

    Again, thank you!
    Studiosa

  6. #6
    Valued Forum Contributor
    Join Date
    07-29-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    534

    Re: Intersection of two sets of values?

    Or use a UDF, see attachment.
    Attached Files Attached Files

  7. #7
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Intersection of two sets of values?

    sure i can break it down
    Range("B10000")
    refers to the cell to start at (for finding the last cell)

    .End(xlUp)
    is just like pressing Ctrl+up in an excel sheet

    .Row
    returns the row numbe of the new reference

    the we do a count if to see if that value exists or not in both columns, then we check if it is already in D, if not, we put it into D and move to the next row

  8. #8
    Registered User
    Join Date
    02-07-2012
    Location
    NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Intersection of two sets of values?

    Wonderful, thank you very much both for the code and the explanation

  9. #9
    Forum Expert icestationzbra's Avatar
    Join Date
    01-07-2004
    MS-Off Ver
    2007, 2010
    Posts
    1,421

    Re: Intersection of two sets of values?

    though i realize this thread is marked as SOLVED, i could not help but chime in with a non-VBA solution for this intersection problem for those that may be looking for one such. please see attached.

  10. #10
    Registered User
    Join Date
    02-07-2012
    Location
    NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Intersection of two sets of values?

    icestationzbra ,
    Thank you very much! At this stage in my learning, I find non-VBA solutions much more comprehensible.

    Studiosa

  11. #11
    Registered User
    Join Date
    06-13-2016
    Location
    Jakarta
    MS-Off Ver
    2003
    Posts
    1

    Re: Intersection of two sets of values?

    thanks, I'll try it

+ 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