Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Compare list in two worksheets

Posted on 2014-03-10
7
Medium Priority
?
140 Views
Last Modified: 2014-07-16
Hi,

I have two worksheets in a workbook that contain a list of IDs, and I need to find a list of IDs that doesn't exist in one.  

I'm looking to compare the "Latest" sheet to "Previous" and look for IDs that are in "Latest" that's missing in "Previous"

Here's the code I'm using below... But I'm not getting the result set I'm expecting

sub get_diff()
Dim wb as workbook, comp_sql as string, comp_rs as new adodb.recordset, new_ws as worksheet 
Dim objconn As New ADODB.Connection

Set wb = ThisWorkbook
Set new_ws = wb.Worksheets("New")

With objconn
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .Properties("Extended Properties").Value = "Excel 8.0;HDR=Yes;IMEX=1"
    .Open wb.FullName
End With

comp_sql = "SELECT a.sid, b.sd " & _
            "FROM [Latest$] as a LEFT JOIN [Previous$] as b ON a.sid = b.sid " & _
            "WHERE b.sid IS NULL "

comp_rs.Open comp_sql, objconn

new_ws.Cells.ClearContents

If comp_rs.State <> 0 Then
    If Not (comp_rs.EOF And comp_rs.BOF) Then
        new_ws.Cells(1, 1).Value = "sid"
        new_ws.Cells(2, 1).CopyFromRecordset comp_rs
    End If
End If

Set comp_rs = Nothing

Open in new window

0
Comment
Question by:iamnamja
  • 3
  • 3
7 Comments
 
LVL 39

Expert Comment

by:nutsch
ID: 39918820
Try:

comp_sql = "SELECT a.sid " & _
            "FROM [Latest$] as a  " & _
            "WHERE a.sid NOT IN (SELECT sid FROM   [Previous$] )"

Thomas
0
 

Author Comment

by:iamnamja
ID: 39918965
Nope... I still get a  random list of ids that shouldn't be showing up...
0
 
LVL 39

Expert Comment

by:nutsch
ID: 39918993
Do you want to post a sample workbook highlighting your issues?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:iamnamja
ID: 39920370
Sure, I've attached the file that contains just the data

as you can see new sheet is where it's supposed to have the difference, but it's showing incorrect results.
sample.xlsx
0
 
LVL 39

Expert Comment

by:nutsch
ID: 39921425
Do you too only get 122 ids coming up?
0
 

Author Comment

by:iamnamja
ID: 39921544
yeah whatever's in the new is what i get as the result
0
 
LVL 1

Accepted Solution

by:
Thymos68 earned 2000 total points
ID: 40050634
I do this sort of comparison task quite often in my business, however I've never considered building code to do it.  (It's so easy to use Excel's formulas to identify the missing IDs, that I've never been motivated to do it any other way.)

I use Excel's COUNTIF formula, to identify the missing ID's and then just filter for them.

In cell B2 on the Latest tab, enter the following:
     =COUNTIF(Previous!A:A,Latest!A2)

Then copy and paste it down your list, and filter the results.

Granted, it's a multi-step process, that isn't as elegant or fast as a bit of code, but I do feel strongly that we lose something of real value, when we further remove the human from the data.   I can't tell you how many times I've stumbled across problems with my clients' supplied data, that would have been completely overlooked, had I created code to automate these simple tasks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question