Solved

Compare list in two worksheets

Posted on 2014-03-10
7
138 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

635 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