|
楼主 |
发表于 2016-10-12 09:01
|
显示全部楼层
Sub test()
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String
Dim mybook As String
mybook = ThisWorkbook.FullName
With cnn
.Provider = "microsoft.jet.oledb.4.0"
.ConnectionString = "extended properties=""excel 8.0;HDR=YES;"";data source=" & mybook
.Open
End With
sql = "select b.船号,b.分段,b.工资 from [sheet1$a1:c] a left outer join [excel 8.0;database=" & ThisWorkbook.Path & "\rsgz.xls].[rsgz$a1:e] b on a.姓名=b.姓名 and a.编号=b.编号"
rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
With Worksheets("sheet1")
For i = 0 To rs.Fields.Count - 1
.Cells(1, i + 4) = rs.Fields(i).Name
Next
.Range("d2").CopyFromRecordset rs
End With
End Sub |
|