对于 300 条记录,这可能是一种过于复杂的方法,但它对于更大的数据集来说是一种有用的技术......
如果您只需要将所有数据集中在一个位置,以便确定要保留哪些描述以及要丢失哪些描述,那么您可以使用 ADO 并将两个数据集连接在一起。
首先转到 Visual Basic 编辑器(按 Alt+F11)。在那里使用工具 > 引用添加对“Microsoft ActiveX 数据对象 2.8 库”的引用
现在插入>模块并粘贴此代码:
Option Explicit
Sub master_list()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
' This is the connection string for .xlsx files (Excel 2007 and later)
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=Excel 12.0 Xml;"
.Open
End With
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM [ssA$] LEFT JOIN [ssB$] ON [ssA$].[svc_itm_cde] = " & _
"[ssB$].[svc_itm_cde] UNION ALL SELECT * FROM [ssA$] RIGHT JOIN [ssB$] ON " & _
"[ssA$].[svc_itm_cde] = [ssB$].[svc_itm_cde] " & _
"WHERE [ssA$].[svc_itm_cde] IS NULL;", cn
Dim i As Integer
Dim fld As ADODB.Field
i = 0
' Sheet3 should be a blank sheet that we can output the results to
With Worksheets("Sheet3")
For Each fld In rs.Fields
i = i + 1
.Cells(1, i).Value = fld.Name
Next fld
.Cells(2, 1).CopyFromRecordset rs
End With
rs.Close
cn.Close
End Sub
如果您使用 Excel 2003 或更早版本,则连接字符串部分应为:
With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=Excel 8.0;"
.Open
End With