SQL链接EXCEL操作

Sub CopyData_5()

Set Cnn = CreateObject("ADODB.Connection")
With Cnn
.Provider = "microsoft.Ace.oledb.12.0"
.ConnectionString = "Extended Properties=Excel 12.0;" _
& "Data Source=" & ThisWorkbook.FullName
.Open
End With
Set rs = CreateObject("ADODB.Recordset")
Sql = "select distinct 经销商名称 from [运营日报$a3:y65536]"
Set rs = Cnn.Execute(Sql)
For i = 0 To rs.Fields.Count - 1
Cells(1, i + 1) = rs.Fields(i).Name
Next
Sheet7.Range("a2").CopyFromRecordset rs
rs.Close
Cnn.Close
Set rs = Nothing
Set Cnn = Nothing
End Sub

posted on 2018-07-18 10:10  上山打老虎下山采蘑菇  阅读(418)  评论(0编辑  收藏  举报

导航