|
- '*********************************
- '******* 北极狐工作室出品 ******
- '******* QQ:14885553 ******
- '*********************************
- Sub Opiona()
- 'On Error Resume Next '// 发生错误,自动执行下一句,就是忽略错误
- Application.ScreenUpdating = False '//关闭屏幕刷新
- Application.DisplayAlerts = False '//关闭系统提示
- t = Timer '//开始时间
- Str1 = "企管01表"
- Set SH1 = Sheets(Str1)
- Set SH2 = Sheets("临时表")
- SH1.Range("C6:Z34").ClearContents
- SH2.Range("A4:Z65536").ClearContents
- arr = FileAllArr(ThisWorkbook.Path, "*.xls", ThisWorkbook.Name)
- For I = 0 To UBound(arr)
- StrCoon = "Provider=Microsoft.jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=NO';Data Source=" & arr(I) '//OFFICE2003
- StrSQL = "SELECT * FROM [" & Str1 & "$A6:Q65536] WHERE LEN(F1)>0"
- If I = 0 Then R = 4 Else R = SH2.Range("A65536").End(3).Row + 1
- SH2.Range("A" & R).CopyFromRecordset GET_SQLRS(StrSQL, StrCoon)
- Next
- StrCoon = "Provider=Microsoft.jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=NO';Data Source=" & ThisWorkbook.FullName
- StrSQL = "SELECT B.BT2,B.BT3,B.BT4,B.BT5,B.BT6,B.BT7 FROM"
- StrSQL = StrSQL & " (SELECT F1 AS BT1 FROM [" & SH1.Name & "$A6:A65536]) AS A LEFT JOIN"
- StrSQL = StrSQL & " (SELECT F1 AS BT1,SUM(F3) AS BT2,SUM(F9)+SUM(F11)+SUM(F13) AS BT3,SUM(F10)+SUM(F12)+SUM(F14) AS BT4,SUM(F15) AS BT5,SUM(F16) AS BT6,SUM(F17) AS BT7 FROM [" & SH2.Name & "$A4:Q65536] WHERE LEN(F1)>0 GROUP BY F1) AS B"
- StrSQL = StrSQL & " ON TRIM(A.BT1)= TRIM(B.BT1)"
- SH1.Range("C6").CopyFromRecordset GET_SQLRS(StrSQL, StrCoon)
- Application.ScreenUpdating = True '//恢复屏幕刷新
- Application.DisplayAlerts = True '//恢复系统提示
- MsgBox "一共用时:" & Format(Timer - t, "#0.0000") & " 秒", , "北极狐提示!!" '//提示所用时间
- End Sub
- '*****************************************************************************************
- '函数名: GET_SQLRS
- '函数功能: 获得指定SQL的查询结果,修改CN连接字符串,可以连接各种数据库
- '返回值: 返回一个recordset数据集
- '参数1: StrSQL 字符类型 SQL查询语句
- '使用方法: Set RS = CreateObject("adodb.recordset") '//先引用ADO:Microsoft ActiveX Data Objects 2.5 或更高版本
- 'Set RS = GET_SQLRS(StrSQL,StrCoon)
- 'Sh1.Range("A2").CopyFromRecordset RS
- '*****************************************************************************************
- Public Function GET_SQLRS(ByVal StrSQL As String, ByVal Str_coon As String) As ADODB.Recordset
- 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
- Set GET_SQLRS = RS
- GET_SQLRS_Exit:
- Exit Function
- GET_SQLRS_Error:
- MsgBox Err.Description
- Resume GET_SQLRS_Exit
- End Function
- '****************************************************************
- '功能: 查找指定文件夹含子文件夹内所有文件名(含路径)
- '函数名: FileAllArr
- '参数1: Filename 需查找的文件夹名 不含最后的""
- '参数2: FileFilter 需要过滤的文件名,可省略,默认为:[*.*]
- '参数3: Liwai 剔除例外的文件名,可省略,默认为:空,一般为:ThisWorkbook.Name
- '返回值: 一个字符型的数组
- '使用方法:arr = FileAllArr(ThisWorkbook.Path, "*.xls", ThisWorkbook.Name)
- Public Function FileAllArr(ByVal Filename As String, Optional ByVal FileFilter As String = "*.*", Optional ByVal Liwai As String = "") 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
-
- I = 0
- Dim arrx() As String
- 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 Function
- '****************************************************************
复制代码 |
|