這可能是做這行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
列R中的內容是否進入目標工作表的兩列(L和M)?看起來像'vlookup()'應該爲此工作。 –
你可能會顯示printscreens?知道你想要達到什麼會更容易。 – ApplePie
vlookup看起來很引人注目,Tim。我可能會放棄這一點。對不起,亞歷山大,我忘了提及:數據不幸是保密的,所以我可以一般性地描述它們,但是打印屏幕會有點過分。我不想惹麻煩。 :( – user1978773