|
原帖由 岁寒三友 于 2010-12-14 12:31 发表
附件里的文件是我看了zhaogang1960老师帮助坛友“风雨之后”时所写的代码所想到的扩展。但我想按照zhaogang1960老师的代码依样画葫芦时,却遇着了难题,即是怎样提取“两表共同拥有”的数据?恳请老师们给予帮助。详 ...
什么时候写的已经不记得了,也看不明白了,下面代码使用ado解决,试试看:
Sub Macro1()
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim SQL$, s$, arr
Set cnn = New ADODB.Connection
cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;imex=1';data source=" & ThisWorkbook.FullName
arr = Sheets("表一").[A1:E1&""]
s = Join(arr, "&") '整行连接字符串
SQL = "Select * from [表一$" & Sheets("表一").[a1].CurrentRegion.Address(0, 0) & "] where " _
& s & " not in (Select " & s & " from [表二$" & Sheets("表二").[a1].CurrentRegion.Address(0, 0) & "])"
Set rs = New ADODB.Recordset
rs.Open SQL, cnn, 1, 3
Range("A1").CurrentRegion.Offset(1).ClearContents
Range("A2").CopyFromRecordset rs '表一独有
Range("f2").Resize(rs.RecordCount) = "√"
SQL = "Select * from [表二$" & Sheets("表二").[a1].CurrentRegion.Address(0, 0) & "] where " _
& s & " not in (Select " & s & " from [表一$" & Sheets("表一").[a1].CurrentRegion.Address(0, 0) & "])"
Set rs = New ADODB.Recordset
rs.Open SQL, cnn, 1, 3
Range("A65536").End(xlUp).Offset(1).CopyFromRecordset rs '表二独有
Range("f65536").End(xlUp).Offset(1, 1).Resize(rs.RecordCount) = "√"
SQL = "Select * from [表一$" & Sheets("表一").[a1].CurrentRegion.Address(0, 0) & "] where " _
& s & " in (Select " & s & " from [表二$" & Sheets("表二").[a1].CurrentRegion.Address(0, 0) & "])"
Set rs = New ADODB.Recordset
rs.Open SQL, cnn, 1, 3
Range("A65536").End(xlUp).Offset(1).CopyFromRecordset rs '两表共同拥有
Range("g65536").End(xlUp).Offset(1, 1).Resize(rs.RecordCount) = "√"
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
End Sub
自动比对重复的记录的拓展2.rar
(18.06 KB, 下载次数: 124)
[ 本帖最后由 zhaogang1960 于 2010-12-15 09:37 编辑 ] |
评分
-
1
查看全部评分
-
|