首页 文章

比较两张excel表

提问于
浏览
2

如何比较两个Excel工作表并确定缺少哪个列?

(我想将表A中的国家列表与表B进行比较,然后标记缺少哪个国家/地区)

注意:它们是随机排列的 .

4 回答

  • 0

    您可以在Excel工作表中使用 VLOOKUP 函数来帮助在不同的工作表中查找"missing"数据 . 例如,请使用以下两个工作表:

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

    .

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

    将以下公式添加到 Sheet 中的单元格 B1 并将公式向下拖动到单元格 B4

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

    Sheet1 应指明 B 列中其他工作表中缺少的项目,如下所示:

    Sheet1
    ------
           A          B                        C
    1     aa 
    2     bb
    3     cc         MISSING FROM OTHER SHEET
    4     dd
    
  • 4

    您可以在Excel中使用ADO

    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
    

    也可以使用CopyFromRecordset快速将记录集写入工作表 .

  • 0

    解决方案取决于所涉及的行数以及您需要执行此操作的次数以及您希望如何呈现信息 .

    如果您没有很多国家/地区而且只需要执行一次,那么最快的解决方案是:

    • 将两列复制到临时表中 .

    • 按字母顺序对两列进行排序 .

    • 手动浏览它们并发现差异 .

    如果您只需要执行一次此操作,但有很多国家/地区,则vlookup选项是最快的选项 .

    如果您需要多次重复此过程,并且需要在某处使用该列表(即在其他工作表中),那么您可以使用更复杂的解决方案,包括两个带查找的附加列和数据透视表 . 但在那时,我会考虑将其转移到更易于管理的地方,比如一个小型数据库 .

  • 0

    我发现,在某些版本的excel中是用于比较文件的功能 - 但通常是禁用的,无论多么严格 . 有一天,我偶然发现了如何使用它 .

    如果你已经安装了TortoiseSVN(真的): - 在文件管理器中选择要比较的excel文件 - 打开上下文菜单,然后选择TortoiseSVN> Diff - 它在“比较模式”中打开excel

相关问题