|
再加上比较表格中不存在的,放到比较表格数据的下方,每比较一个工作簿,会输出3列数据:工作簿、A列数据、B列数据- Sub Macro1()
- Dim cnn As Object, MyPath$, MyFile$, SQL$, m&, sh As Worksheet, s$
- Application.ScreenUpdating = False
- Set sh = ActiveSheet
- s = "[Excel 8.0;hdr=no;Database=" & ThisWorkbook.FullName & "].[" & sh.Name & "$a1:a" & [a65536].End(xlUp).Row & "]"
- sh.UsedRange.Offset(, 2).ClearContents
- Set cnn = CreateObject("adodb.connection")
- MyPath = ThisWorkbook.Path & ""
- MyFile = Dir(MyPath & "*.xls")
- m = -1
- With sh
- Do While Len(MyFile)
- If MyFile <> ThisWorkbook.Name Then
- m = m + 3
- If m = 2 Then
- cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no';data source=" & MyPath & MyFile
- SQL = "select '" & Split(MyFile, ".")(0) & "工作簿',a.* from [Sheet1$] a right join " & s & " b on a.f1=b.f1 union all " _
- & "select '" & Split(MyFile, ".")(0) & "工作簿',a.* from [Sheet1$] a left join " & s & " b on a.f1=b.f1 where b.f1 is null"
- Else
- SQL = "select '" & Split(MyFile, ".")(0) & "工作簿',a.* from [Excel 8.0;hdr=no;Database=" & MyPath & MyFile & "].[Sheet1$] a right join " & s & " b on a.f1=b.f1 union all " _
- & "select '" & Split(MyFile, ".")(0) & "工作簿',a.* from [Excel 8.0;hdr=no;Database=" & MyPath & MyFile & "].[Sheet1$] a left join " & s & " b on a.f1=b.f1 where b.f1 is null"
- End If
- .Cells(1, m).CopyFromRecordset cnn.Execute(SQL)
- End If
- MyFile = Dir
- Loop
- End With
- cnn.Close
- Set cnn = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|