Microsoft Excel - Finding records that don't match

Asked By J D
06-Jun-09 10:10 AM

I have two separate Excel worksheets containing three fields which are common (but each contains other fields).  There is no field which uniquely identifies a record in either worksheet - even the combination of those three fields wouldn't uniquely identify a specific record.  I need to match records in worksheet A to worksheet B and identify those records in A which do not have a match in B (on the three common fields).

I tried solving this with VBA and also through an SQL statement (after importing the worksheets into tables in Access).  I'm stumped on what's the best way to accomplish this.  Any help would be extrememly appreciated!

Thanks in advance!
Joe

VBA to compare sheets  VBA to compare sheets

06-Jun-09 04:40 PM

Hi Joe,

This code will evaluate 2 fields and find matches or fields that don't match depending on your needs.  Next, add the unmatched record to a new sheet or bold the record in code to identify records.  Download zip file containing the code at www.jettechnical.com/sampletutorials/checkrecords.zip

Function FindUnmatchedRecords()
Dim strCompare As String
Dim strCompare1 As String
    'set up the sheet you will be comparing to on the
    'first cell to be checked
    Range("A2").Select
    'get the first sheet
    Sheets("Sheet1").Select
    'alter this to the column you want to compare.
    'this assumes that row 1 will be the header
    Range("A2").Select
    'do until the field is empty
    Do Until ActiveCell.Value = ""
        strCompare = ActiveCell.Value
        Sheets("Sheet2").Select
        strCompare2 = ActiveCell.Value
        If strCompare2 <> strCompare Then
            Sheets("Sheet3").Select
            ActiveCell.Value = strCompare2
            'set up for the next record
            ActiveCell.Offset(1, 0).Select
            'go back to Sheet2
            Sheets("Sheet2").Select
        End If
        'move to the next cell
        ActiveCell.Offset(1, 0).Select
        Sheets("Sheet1").Select
        'move to the next cell
        ActiveCell.Offset(1, 0).Select
    Loop
      

End Function

To better understand how this works, open the project, open the macro (ALT + F11) , minimize the macro window so that you can see both the spreadsheet and the macro window.  Place the cursore in the function and press F8 to step through the code and what how it executes.  Then you'll be able to modify as you need.

Happy Coding!

Robert Pattinson

 

 

 

Create New Account
help
How can I make my data show as flashing in Excel? Excel Excel Miscellaneous Discussions Microsoft Excel (1) Excel (1) Application.OnTime (1) Worksheet (1) Workbook (1) Macro (1) VBA (1) Font.ColorIndex (1 if you insist, see Chip Pearson's site for VBA code. http: / / www.cpearson.com / excel / BlinkingText.aspx Gord Dibben MS Excel MVP This was great - thank you. My boss loved it. Now I have another question
EXCEL SHEETS Excel Can you reinstate deleted sheets in excel, if so how please. thanks Excel Worksheet Discussions Microsoft Excel (1) Worksheet (1) Workbook (1) Daisy (1) Closeit (1) If you haven't saved the
Non synchronisation in split screens in Microsoft Excel Excel Lotus 123 has the ability to split a worksheet in two, similar to Microsoft Excel, but in Lotus 123 you have the ability to scroll down one side whilst the other side remains stationary. Can you do that in Microsoft Excel and if so how? Excel Miscellaneous Discussions Microsoft Excel (1) Worksheet (1) Workbook (1) VB (1) Normek (1) Lotus (1
No colours on excel sheets Excel Excel 2003 No colours show up if i select a colour for figures, letters, frames etc. When printing all colours appear correctly. Which button I pressed by mistake? Excel Crashes GPFs Discussions Excel (1) Windows (1) If this is a problem with all Excel files with coloured cells, the Windows OS high contrast setting may be turned on. There MSKB article: OFF: Changes to Fill Color and Fill Pattern Are Not Displayed http: / / support.microsoft.com / default.aspx?scid = kb;en-ca;320531 - - Gary''s Student - gsnu200715 keywords: No, colours
Opening and retrieving information from numerous excel sheets Excel I am currently trying to do work with numerous different excel sheets. I am looking for the user to open a blank Excel worksheet and click a button. This button will make a dialogue box appear and ask a cell from each sheet opened (for example cell A1). How would I do this? Excel Programming Discussions SQL Server 2000 (1) Office 2003 (1) Office (1) Excel (1) Worksheet (1) VbMaximizedFocus (1) BZeyger (1) Rogers (1) Here's something to get you