|
楼主 |
发表于 2012-12-28 10:05
|
显示全部楼层
非常感谢各位老师
sheet1:
名称 数量 日期 号码
a 1 2012-4-2 0011223
b 2 2012-5-2 0011224
c 1 2012-6-2 0011225
a 1 2012-7-2 0011226
b 3 2012-8-2 0011227
c 5 2012-9-2 0011228
sheet2
名称 数量 日期 号码
a 1 2012-4-2 S0011223
b 2 2012-5-2 S0011224
c 1 2012-6-2 S0011225
a 1 2012-7-2 S0011226
c 5 2012-9-2 S0011228
用Renco的SQL那句
查询的结果为:
名称 数量 日期 号码 号码2
a 1 2012-4-2 0011223 S0011223
b 2 2012-5-2 0011224 S0011224
c 1 2012-6-2 0011225 S0011225
a 1 2012-7-2 0011226 S0011223
b 3 2012-8-2 0011227
c 5 2012-9-2 0011228 S0011228
如果没有 b 3 2012-8-2 0011227这条记录就完美了,就是我想要的数据
其实我就是对账用的,表1的一条记录名称和数量与表2的名称数量相同一条记录对上了,就把两表相同的记录查询出来,并且用表2的单据号码写过来作为核对标志,就OK了。这样我就知道表1 的记录对应的是表2的那条记录(通过写过来的单据号码知道)
Dim CNN1 As New ADODB.Connection
Dim CNN2 As New ADODB.Connection
Dim RS1 As New ADODB.Recordset
Dim RS2 As New ADODB.Recordset
Dim i%
Set xlapp = CreateObject("Excel.Application")
Set CNN1 = New ADODB.Connection
CNN1.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\cs1.xlsx; Extended Properties='Excel 12.0;HDR=YES'"
CNN1.Open
If CNN1.State = adStateOpen Then
MsgBox "连接成功"
Else
MsgBox "连接失败"
End If
With xlapp
' .Visible = True
Sql = "select A.*,B.号码 as 号码2 FROM [sheet1$] A LEFT JOIN (Select [sheet2$].名称,[sheet2$].数量,first([sheet2$].号码) as 号码 From [sheet2$] Group by [sheet2$].名称,[sheet2$].数量) B ON A.名称=B.名称 AND A.数量=B.数量"
Set RS1 = New ADODB.Recordset
RS1.Open Sql, CNN1, adOpenStatic, adLockOptimistic
Application.ScreenUpdating = False
Range("a1:f30").ClearContents
For i = 0 To RS1.Fields.Count - 1
Cells(1, i + 1) = RS1.Fields(i).Name
Next i
Range("a2").CopyFromRecordset RS1
End With
Application.ScreenUpdating = True
|
|