+ Reply to Thread
Results 1 to 4 of 4

I need to compare 2 large lists of numbers and out put numbers that are in both lists.

Hybrid View

  1. #1
    Registered User
    Join Date
    11-21-2016
    Location
    Anchorage, AK
    MS-Off Ver
    MS Office Professional 2013
    Posts
    1

    I need to compare 2 large lists of numbers and out put numbers that are in both lists.

    Hi everyone thanks for taking the time to help out.

    I have two databases in excel I need to compare. One contains the serial numbers of a bunch of items that are assigned to my organization. The other contains the serial numbers of all the items in my organization that are registered for maintenance. Over time these two lists no longer match.

    I can easily put all the data in one workbook in two separate columns. What I want to do is run a macro that can compare these to lists and give three outputs which are serial numbers that are:

    1) Only in list A.
    2) Only in list B.
    3) In both lists A and B.

    Thanks for the help!

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: I need to compare 2 large lists of numbers and out put numbers that are in both lists.

    To help to prepare the macro and be sure to handle the data at the right place, it will very good to prepare an Excel sample of the data showing how, where the final result stands: Attach the sample in the thread
    - Battle without fear gives no glory - Just try

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: I need to compare 2 large lists of numbers and out put numbers that are in both lists.

    Maybe:

    Sub imcquill09()
    Dim i As Long, x As Range, y As Long
    y = Range("B" & Rows.Count).End(3).row
    For i = 2 To Range("A" & Rows.Count).End(3).row
        Set x = Columns(2).Find(Cells(i, "A"), LookIn:=xlValues, lookat:=xlWhole)
            If Not x Is Nothing Then
                Cells(Rows.Count, "E").End(3)(2) = Cells(i, "A")
                x.Interior.ColorIndex = 6
            Else
                Cells(Rows.Count, "C").End(3)(2) = Cells(i, "A")
            End If
        Set x = Nothing
    Next i
    For Each x In Range(Cells(2, 2), Cells(y, 2))
        If x.Interior.ColorIndex = xlNone Then
            x.Copy Range("D" & Rows.Count).End(3)(2)
        Else
           x.Interior.ColorIndex = xlNone
        End If
    Next x
                
    End Sub
    Comparing Columns A and B.
    Output 1) - Column C
    Output 2) - Column D
    Output 3) - Column E

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: I need to compare 2 large lists of numbers and out put numbers that are in both lists.

    This ought to do the trick... you will need to play with the columns probably... this assumes source sheet has list A in col A, and list B on Col B of "Sheet1"
    It then outputs Only in A in col A, only in B in col B, and what is in both in col C. Output sheet is "Sheet2"

    Sub compare()
    
    Dim arr As Variant
    Dim varr As Variant
    Dim wss As Worksheet
    Dim wsd As Worksheet
    Dim x, y
    Dim match As Boolean
    Dim i As Long, j As Long
    
    Application.ScreenUpdating = False
    
    Set wss = Worksheets("Sheet1") 'sheet with source data
    Set wsd = Worksheets("Sheet2") 'sheet with output columns
    
    arr = wss.Range("A2:A" & wss.Range("A" & Rows.Count).End(xlUp).Row).Value 'starts at A2 assuming a header
    varr = wss.Range("B2:B" & wss.Range("B" & Rows.Count).End(xlUp).Row).Value 'starts at B2 assuming a header
    i = 1 'will start the list of values only in A and only in B on row 2 because later we add 1 before writing
    j = 1 'will start the list of values that are in both lists on row 2 because later we add 1 before writing
    'here we compare lists, write what is only in column A, and what is common
    For Each x In arr
        match = False
        For Each y In varr
            If x = y Then 'there is a match in col A vs Col B
                match = True
                j = j + 1
                wsd.Range("C" & j).Value = x 'write the value of X in the list of common values
            End If
        Next y
        If Not match Then
            i = i + 1
            wsd.Range("A" & i).Value = x 'write the value in the list of items in A but not in B
        End If
    Next
    'now get the list only in B
    i = 1
    For Each x In varr
        match = False
        For Each y In arr
            If x = y Then match = True
        Next y
        If Not match Then
            i = i + 1
            wsd.Range("B" & i).Value = x 'write the value in the list of items in A but not in B
        End If
    Next
    
    Application.ScreenUpdating = True
    End Sub
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 8
    Last Post: 11-19-2014, 06:12 PM
  2. Identify missing numbers in a list
    By jcad123el01 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-18-2014, 11:40 PM
  3. [SOLVED] 2 lists: how to compare and calculate difference in item numbers
    By chemoul in forum Excel General
    Replies: 5
    Last Post: 02-15-2013, 12:59 AM
  4. Comparing numbers in lists/columns of numbers
    By Onesimus in forum Excel General
    Replies: 3
    Last Post: 12-15-2007, 02:19 PM
  5. Compare and restrict available numbers in two lists
    By jpp8596 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-21-2007, 01:54 PM
  6. Replies: 7
    Last Post: 07-13-2006, 06:15 PM
  7. How do I compare two lists of numbers?
    By demebe123 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-21-2005, 08:07 PM

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