Solved

Compare list in two worksheets

Posted on 2014-03-10
7
137 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

751 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