Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have two excel sheets with same column Names and Format. And it has one identical row(can use as Primary Key) id. I want to get the diff.

table 1:

 id   Name   GPA 
----+------+-------
 1  | AA   |  3
 2  | BB   |  2
 3  | CC   |  3
 4  | DD   |  1

Table 2:

 id   Name   GPA 
----+------+-------
 4  | DD   |  2               (updated)
 7  | YY   |  2               (New)
 1  | AA   |  3                  _
 2  | DD   |  2               (Updated)  

Result Table:

     id   Name   GPA 
    ----+------+-------
     4  | DD   |  2               
     7  | YY   |  2                    
     2  | DD   |  2   

I want to populate the result table with only Updated and New Rows. (We can identify row using Primary key)

Is there any function directly to get the difference in Excel(VLOOKUP) ?

If not how can I write a query in Access to get this done?

share|improve this question

migrated from stackoverflow.com Oct 30 '12 at 8:47

This question came from our site for professional and enthusiast programmers.

    
Do you want to call out the updated records and the new records? or do you just want to get a total list of different things? –  Brad Oct 30 '12 at 14:45

2 Answers 2

Option Explicit

Sub PutChangedRecordsIntoSomewhere()
    Dim rs As ADODB.Recordset
    Set rs = FindChangedRecords(ThisWorkbook.Path & "\" & ThisWorkbook.Name)
    Dim destSheet As Worksheet
    Set destSheet = Sheets("Sheet3")
    destSheet.Range("A2").CopyFromRecordset rs

    rs.Close
    Set rs = Nothing
End Sub



Public Function FindChangedRecords(WorkbookPath As String) As ADODB.Recordset

    Dim rst As New ADODB.Recordset
    Dim cnx As New ADODB.Connection
    Dim cmd As New ADODB.Command

    'setup the connection
    With cnx
        On Error Resume Next
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .ConnectionString = "Data Source='" & WorkbookPath & "'; " & "Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'"
        .Open
        If Err.Number <> 0 Then
            MsgBox Err.Description
            Set FindChangedRecords = Nothing
            Exit Function
        End If
        On Error GoTo 0
    End With

    'setup the command
    Set cmd.ActiveConnection = cnx
    cmd.CommandType = adCmdText
    cmd.CommandText = "Select s2.* " & _
                "from [Sheet2$] s2 " & _
                "left join [Sheet1$] s1 on s1.id = s2.id and s1.name = s2.name and s1.gpa = s2.gpa " & _
                "where s1.id is null"   '<-- change sheet2 to where your "table2" is
                                        '<-- change sheet1 to where your "table1" is

    rst.CursorLocation = adUseClient
    rst.CursorType = adOpenDynamic
    rst.LockType = adLockOptimistic
    'open the connection
    rst.Open cmd

    'disconnect the recordset
    Set rst.ActiveConnection = Nothing
    'cleanup
    If CBool(cmd.State And adStateOpen) = True Then
        Set cmd = Nothing
    End If

    If CBool(cnx.State And adStateOpen) = True Then cnx.Close
    Set cnx = Nothing

    'return the recordset object
    Set FindChangedRecords = rst


End Function
share|improve this answer

Assuming your data is on Sheet1, the attached formula will give you results of New/ Updated or Blank if you copy it to the right of your table 2.

=IF(ISNA(MATCH(A2,Sheet1!$A:$A,0)),"New",IF(OR(VLOOKUP($A2,Sheet1!$A:$C,2,0)<>$B2,VLOOKUP($A2,Sheet1!$A:$C,3,0)<>$C2),"Updated",""))

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.