Solved

Compare list in two worksheets

Posted on 2014-03-10
7
136 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
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 Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

830 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