|
本帖最后由 zhaogang1960 于 2014-7-13 17:45 编辑
数据表中的Builder_ID字段有重复值,下面程序仅查询第一条Builder_ID所对应的记录(请注意,D列数据不能超过4500行,否则会出现错行):- Sub 宏1()
- Dim cnn As Object
- Dim SQL As String
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\data of shipping.accdb"
- SQL = "select b.Acquired_Price,b.Builder,b.Builder_CoD from [Excel 12.0;Database=" & ThisWorkbook.FullName _
- & ";].[" & ActiveSheet.Name & "$d1:d" & Range("d" & Rows.Count).End(xlUp).Row _
- & "] a left join (select Acquired_Price,Builder,Builder_CoD,first(Builder_ID) as Builder_ID from Sheet1 group by Acquired_Price,Builder,Builder_CoD) b on a.Builder_ID=b.Builder_ID"
- Range("A2").CopyFromRecordset cnn.Execute(SQL)
- cnn.Close
- Set cnn = Nothing
- End Sub
复制代码 |
|