|
本帖最后由 cmo9020 于 2024-3-29 12:11 编辑
请教导师们有关sql代码取目标工作表数值问题
目标档工作表"FDE31983"和"FDE31984"有重复选项,
执行代码会把重复数据都会全部复制.....这不我想要的
我要的是取唯一的最后一笔数值,要如何修改?谢谢~
test.rar
(29.26 KB, 下载次数: 10)
Sub test1()
Dim Cn As Object, ar, i&, p$, f$, Sq$(2), s$
Set Cn = CreateObject("ADODB.Connection")
If Application.Version < 12 Then
s = "Excel 8.0;Database="
Cn.Open "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullName
Else
Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
s = "Excel 12.0;Database="
End If
With Worksheets(1)
.Activate
Sq(0) = "[" & .Name & "$g1:g" & .Cells(.Rows.Count, "g").End(xlUp).Row & "]a"
End With
p = ThisWorkbook.Path & "\"
ar = Array("scra.xls", "SHIPP.xls")
For i = 0 To UBound(ar)
f = p & ar(i)
If Dir(f, vbDirectory) <> "" Then
Sq(1) = Sq(1) & " UNION ALL SELECT 选项号码,料号,品名 FROM [" & s & f & "].[石中$A1:K] WHERE 选项号码 IS NOT NULL"
End If
Next
Sq(2) = "SELECT b.料号,b.品名 FROM " & Sq(0) & " LEFT JOIN (" & Mid(Sq(1), 12) & ")b ON a.选项号码=b.选项号码"
Range("h2").CopyFromRecordset Cn.Execute(Sq(2))
Cn.Close
Set Cn = Nothing
End Sub
|
|