首页 > 编程语言 > 详细

VBA利用ADO操作EXCEL表

时间:2019-10-29 23:55:31      阅读:160      评论:0      收藏:0      [点我收藏+]

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

VBA利用ADO操作EXCEL表

原文:https://www.cnblogs.com/zhujie-com/p/11762044.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!