码迷,mamicode.com
首页 > 编程语言 > 详细

VBA利用ADO操作EXCEL表

时间:2019-10-29 23:56:38      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:copy   小数   object   VID   字段   extend   string   lsm   exce   

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表

标签:copy   小数   object   VID   字段   extend   string   lsm   exce   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!