Solved

Compare list in two worksheets

Posted on 2014-03-10
7
131 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now