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

原文地址:https://www.cnblogs.com/shida-liu/p/9327590.html