【问题标题】:Compare two excel sheets比较两张excel表
【发布时间】:2010-06-16 08:14:40
【问题描述】:

如何比较两个excel表并确定缺少哪一列?

(我想比较工作表 A 和工作表 B 中的国家/地区列表,然后标出缺少哪个国家/地区)

注意:它们的顺序是随机的。

【问题讨论】:

标签: excel excel-2007 compare vba


【解决方案1】:

您可以在 Excel 工作表中使用 VLOOKUP 函数来帮助在不同的工作表中查找“缺失”的数据。例如,以下两个工作表:

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

【讨论】:

    【解决方案2】:

    您可以在 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 将记录集快速写入工作表。

    【讨论】:

      【解决方案3】:

      解决方案因所涉及的行数、您需要执行此操作的次数以及您希望信息的呈现方式而异。

      如果您的国家/地区不多且只需执行一次,最快的解决方案是:

      • 将两列复制到临时工作表中。
      • 按字母顺序对两列进行排序。
      • 手动检查它们并找出差异。

      如果您只需要执行一次,但国家/地区很多,vlookup 选项是最快的。

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

      【讨论】:

        【解决方案4】:

        我发现,在某些版本的 excel 中,用于比较文件的功能 - 但通常被禁用,但安装。有一天,我偶然发现了如何使用它。

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

        【讨论】:

          猜你喜欢
          • 2011-10-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-08
          相关资源
          最近更新 更多