|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
还是来个附件看看吧!!
- '*********************************
- '******* 北极狐工作室出品 ******
- '******* QQ:14885553 ******
- '*********************************
- Sub Opiona() '//函数实例
- Set Sh0 = Worksheets("汇总")
- Arr = FileAllArr(ThisWorkbook.Path, "*.xls", ThisWorkbook.Name, False)
- For i = 0 To UBound(Arr)
- Irow = Sh0 / Range("A65536").End(3).Row + 1
- Str_coon = "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=no';data source=" & Arr(i)
- StrSQL = "SELECT * FROM [Sheet1$a9:V9]"
- Arr = GET_SQLCoon(StrSQL, Str_coon, False)
- Sh0.Range("A" & Irow).Resize(UBound(Arr, 1) + 1, UBound(Arr, 2) + 1) = Arr
- Next
- End Sub
- 'CN.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';data source=" & ThisWorkbook.FullName '//连接Excel2007
- 'CN.Open "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=yes';data source=" & ThisWorkbook.FullName '//OFFICE2003
- '*****************************************************************************************
- '函数名: GET_SQLCoon
- '函数功能: 获得指定SQL的查询结果,自定义连接字符串,可以连接各种数据库
- '返回值: 返回一个二维数组
- '参数1: StrSQL 字符类型 SQL查询语句
- '参数2: Str_coon 字符类型 数据库连接语句
- '参数3: Biaoti 可参数选 是否输出标题,默认带有标题
- '使用方法: Arr = GET_SQLCoon(StrSQL,Str_coon,true)
- ' Arr(0,1) '//数组第一行为标题行,从i=1 开始是数据
- ' Sh2.Range("A2").Resize(UBound(ARR, 1) + 1, UBound(ARR, 2) + 1) = ARR
- '*****************************************************************************************
- Public Function GET_SQLCoon(ByVal StrSQL As String, ByVal Str_coon As String, Optional Biaoti As Boolean = True) As Variant()
- On Error Resume Next ' 改变错误处理的方式。
- Dim CN, RS
- Err.Clear
- Set CN = CreateObject("Adodb.Connection") '//新建一个ADO连接
- Set RS = CreateObject("adodb.recordset")
- CN.Open Str_coon
- RS.Open StrSQL, CN, 1, 3
- If RS.RecordCount > 0 Then '//如果找到数据
- If Biaoti = True Then
- ReDim Arr(0 To RS.RecordCount, 0 To RS.Fields.Count - 1)
- For a = 0 To RS.Fields.Count - 1 '//导入标题
- Arr(0, a) = RS.Fields(a).Name
- Next
- For i = 0 To RS.RecordCount - 1 '//导入数据
- For a = 0 To RS.Fields.Count - 1
- Arr(i + 1, a) = RS.Fields(a).Value
- Next a
- RS.MoveNext
- Next
- Else
- ReDim Arr(0 To RS.RecordCount - 1, 0 To RS.Fields.Count - 1)
- For i = 0 To RS.RecordCount - 1 '//导入数据
- For a = 0 To RS.Fields.Count - 1
- Arr(i, a) = RS.Fields(a).Value
- Next a
- RS.MoveNext
- Next
- End If
- Else '//如果没有找到数据
- ReDim Arr(1, 1)
- Arr(0, 0) = ""
- End If
- GET_SQLCoon = Arr
- CN.Close '//关闭ADO连接
- Set RS = Nothing
- Set CN = Nothing '//释放内存
- End Function
- '*******************************************************************************************************
- '功能: 查找指定文件夹含子文件夹内所有文件名或文件夹名(含路径)
- '函数名: FileAllArr
- '参数1: Filename 需查找的文件夹名 不含最后的""
- '参数2: FileFilter 需要过滤的文件名,可省略,默认为:[*.*]
- '参数3: Liwai 剔除例外的文件名,可省略,默认为:空,一般为:ThisWorkbook.Name
- '参数4: Files 是否只要文件夹名,可省略,默认为:FALSE
- '返回值: 一个字符型的数组
- '使用方法:arr = FileAllArr(ThisWorkbook.Path, "*.xls", ThisWorkbook.Name,false)
- '作者: 北极狐工作室 QQ:14885553
- '*******************************************************************************************************
- Public Function FileAllArr(ByVal Filename As String, Optional ByVal FileFilter As String = "*.*", Optional ByVal Liwai As String = "", Optional ByVal Files As Boolean = False) As String()
- Set Dic = CreateObject("Scripting.Dictionary") '创建一个字典对象
- Set Did = CreateObject("Scripting.Dictionary")
- Dic.Add (Filename & ""), ""
- i = 0
- Do While i < Dic.Count
- Ke = Dic.keys '开始遍历字典
- MyName = Dir(Ke(i), vbDirectory) '查找目录
- Do While MyName <> ""
- If MyName <> "." And MyName <> ".." Then
- If (GetAttr(Ke(i) & MyName) And vbDirectory) = vbDirectory Then '如果是次级目录
- Dic.Add (Ke(i) & MyName & ""), "" '就往字典中添加这个次级目录名作为一个条目
- End If
- End If
- MyName = Dir '继续遍历寻找
- Loop
- i = i + 1
- Loop
- Dim arrx() As String
- i = 0
- If Files = True Then '//是否只输出文件夹名
-
- For Each Ke In Dic.keys '以查找总表所在文件夹下所有excel文件为例
- ReDim Preserve arrx(i)
- If Ke <> Filename & "" Then '//自身文件夹除外
- arrx(i) = Ke
- i = i + 1
- End If
- Next
- FileAllArr = arrx
- Else
- For Each Ke In Dic.keys '以查找总表所在文件夹下所有excel文件为例
- MyFileName = Dir(Ke & FileFilter) '过滤器:EXCEL2003为:*.xls,excel2007为:*.xlsx
- Do While MyFileName <> ""
- If MyFileName <> Liwai Then '排除例外文件
- ReDim Preserve arrx(i)
- arrx(i) = Ke & MyFileName
- i = i + 1
- End If
- MyFileName = Dir
- Loop
- Next
- FileAllArr = arrx
- End If
- End Function
- '****************************************************************
复制代码 |
评分
-
1
查看全部评分
-
|