Sub OPENSANDEXC()
Dim Conn As Object, Rst As Object
Dim sql As String, Path As String
Dim i As Integer, PathStr As String
Set Conn = CreateObject("ADODB.Connection")
Set Rst = CreateObject("ADODB.Recordset")
Path = "H:\应付账款9月.xlsm" ‘要操作的工作薄路径
Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties=‘Excel 8.0;HDR=Yes;IMEX=1‘;" ‘Office 07版本以上
‘Conn.Open "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Path + ";Extended Properties=‘Excel 8.0;HDR=NO;IMEX=1‘;" ‘Office 07版本以下
sql = "select [编 码],[名称#] from [工作表1$]" ‘如果字段名中包含空格,在SQL语句中要用中括号括起来,如果有小数点(.),要用#号代替并用中括号括起来
Set Rst = Conn.Execute(sql)
For i = 0 To Rst.Fields.Count - 1 ‘字段
Cells(1, i + 1) = Rst.Fields.Item(i).Name
Next
Range("a2").CopyFromRecordset Rst ‘复制记录集
Conn.Close
Set Conn = Nothing: Set Rst = Nothing
End Sub
原文地址:https://www.cnblogs.com/zhujie-com/p/11762044.html