ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > Excel VBA > 如何使用VBA+SQL+ADO汇总当前文件中所有工作簿?

如何使用VBA+SQL+ADO汇总当前文件中所有工作簿?

作者:绿色风 分类: 时间:2022-08-17 浏览:102
楼主
wise
Q:如何使用VBA+SQL+ADO汇总当前文件中所有工作簿?
A:ALT+F11→插入模块→在模块中输入以下代码:
  1. Sub test()
  2.   Dim cnn As Object, SQL As String, MyPath$, MyFile$, n As Long
  3.   Cells.Clear
  4.   Range("A1:D1") = Array("单位", "工资帐号", "姓名", "实发工资")
  5.   '赋值给A1:D1
  6.   Set cnn = CreateObject("ADODB.Connection")
  7.   '建立ADO的后绑定
  8.   MyPath = ThisWorkbook.Path & "\"
  9.   '获取当前路径
  10.   MyFile = Dir(MyPath & "*.xlsx")
  11.   '获取当前路径中的xlsx文件
  12.   Do While MyFile <> ""
  13.   '如果找到的文件不为空
  14.      If MyFile <> ThisWorkbook.Name Then
  15.      '如果文件不等于当前文件名
  16.         n = n + 1
  17.         '累加文件次数
  18.         If n = 1 Then
  19.            cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & MyPath & MyFile
  20.            SQL = "SELECT * FROM [Sheet1$A1:D200] WHERE 姓名 IS NOT NULL"
  21.            '如果只有一个文件,则直接连接
  22.         Else
  23.            SQL = SQL & " UNION ALL SELECT * FROM [Excel 12.0;Database=" & MyPath & MyFile & "].[Sheet1$A1:D200] WHERE 姓名 IS NOT NULL "
  24.            '否则直接关联
  25.         End If
  26.      End If
  27.     MyFile = Dir()
  28.     '继续获取文件
  29.   Loop
  30.   Range("A1").CurrentRegion.Offset(1, 0).ClearContents
  31.   Range("A2").CopyFromRecordset cnn.Execute(SQL)
  32.   '执行SQL
  33.   cnn.Close
  34.   Set cnn = Nothing
  35. End Sub


VBA+SQL+ADO.rar
2楼
ljh29206
wise的 sql 真的炉火纯青!
3楼
kszcs
在2003上不能运行吗?
4楼
wise
  1. cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & MyPath & MyFile
03的需要修改这里
5楼
adamsky
Sub abb()
Dim cnn As Object, sql As String, mypath$, myfile$, n As Long
Cells.Clear
Range("a1:d1") = Array("单位", "工资", "姓名", "实发工资")
Set cnn = CreateObject("ADODB.CONNECTION")
mypath = ThisWorkbook.Path & "\"
myfile = Dir(mypath & "*.xlsx")
    Do While myfile <> ""
        If myfile <> ThisWorkbook.Name Then
            n = n + 1
                If n = 1 Then
                    cnn.Open "provider=microsoft.ace.oledb.12.0;extended properties =excel 12.0;data source =" & mypath & myfile
                    sql = "select * from [sheet1$a1:d200] where 姓名 is not null"
                Else
                    sql = sql & " union all select * from [excel 12.0;database =" & mypath & myfile & "].[sheet1$a1:d200] where 姓名 is not null"
                    
                End If
        End If
    myfile = Dir()
    Loop
        Range("a1").CurrentRegion.Offset(1, 0).ClearContents
        Range("a2").CopyFromRecordset cnn.Execute(sql)
    cnn.Close
    Set cnn = Nothing
End Sub

怎么老是提示不能更新,数据库或对象为只读??
6楼
喜宝
学无止境啊

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap