Compare two excel sheets

前端 未结 4 1834
悲哀的现实
悲哀的现实 2020-12-16 08:20

How do I compare two excel sheet and determine which column is missing?

(I would like to compare a list of countries from sheet A with sheet B, then mark which count

相关标签:
4条回答
  • 2020-12-16 08:46

    You can use ADO with Excel

    Dim cn As Object
    Dim rs As Object
    Dim strFile As String
    Dim strCon As String
    Dim strSQL As String
    Dim s As String
    Dim i As Integer, j As Integer
    
    ''This is not the best way to refer to the workbook
    ''you want, but it is very conveient for notes
    ''It is probably best to use the name of the workbook.
    
    strFile = ActiveWorkbook.FullName
    
    ''Note that if HDR=No, F1,F2 etc are used for column names,
    ''if HDR=Yes, the names in the first row of the range
    ''can be used. 
    ''This is the Jet 4 connection string, you can get more
    ''here : http://www.connectionstrings.com/excel
    
    strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
        & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
    
    ''Late binding, so no reference is needed
    
    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    
    
    cn.Open strCon
    
     ''Query example:
     strSQL = "SELECT Country " _
           & "FROM [Sheet1$] a " _
           & "WHERE Country NOT IN " _
           & "SELECT Country FROM [Sheet2$]"
    
    
    ''Open the recordset for more processing
    ''Cursor Type: 3, adOpenStatic
    ''Lock Type: 3, adLockOptimistic
    ''Not everything can be done with every cirsor type and 
    ''lock type. See http://www.w3schools.com/ado/met_rs_open.asp
    
    rs.Open strSQL, cn, 3, 3
    
    If rs.Count>0 Then
        MsgBox rs.GetString
    End If
    

    It is also possible to quickly write the recordset to a sheet with CopyFromRecordset.

    0 讨论(0)
  • 2020-12-16 08:50

    The solution varies depending on the number of rows involved and the number of times you need to do this, and how you want the information to be presented.

    If you don't have a lot of countries and you need to do this only once, the fastest solution is:

    • Copy both columns into a temporary sheet.
    • Sort both columns alphabetically.
    • Manually go through them and spot the differences.

    If you need to do this just once, but there are lots of countries, the vlookup option is the fastest one.

    If you need to repeat this procedure lots of times, and you need use that list somewhere (i.e. in other sheet) then you can use a more convoluted solution, involving two additional columns with lookups, and pivot tables. But at that point I'd look at moving it to something more manageable, like a small database.

    0 讨论(0)
  • 2020-12-16 09:04

    I found, that in some versions of excel is feature for comparing files - but commonly is disabled, however instaled. And one day I found by chance how use it.

    If you have instaled TortoiseSVN (really): - select excel files to compare in your file manager - open context menu, then select TortoiseSVN > Diff - it opens excel in "comparing mode"

    0 讨论(0)
  • 2020-12-16 09:11

    You can use the VLOOKUP function in an Excel worksheet to help finding "missing" data in a different sheet. For example, take the following two worksheets:

    Sheet1
    ------
           A          B         C
    1     aa 
    2     bb
    3     cc 
    4     dd
    

    .

    Sheet2
    ------
           A          B         C
    1     aa 
    2     bb
    3     dd 
    

    Add the following formula to cell B1 in Sheet and drag the formula down through cell B4:

    =IF(ISERROR(VLOOKUP(A1,Sheet2!$A$1:$A$3,1,FALSE)),"MISSING FROM OTHER SHEET","")
    

    Sheet1 should indicate items that are missing from the other sheet in column B, like so:

    Sheet1
    ------
           A          B                        C
    1     aa 
    2     bb
    3     cc         MISSING FROM OTHER SHEET
    4     dd
    
    0 讨论(0)
提交回复
热议问题