|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub OPIONA() '使用双字典,旨在提高速度
- Dim MyName, Dic, Did, I, T, F, TT, MyFileName
- Application.ScreenUpdating = False '关闭屏幕刷新
- Application.DisplayAlerts = False '关闭提示
- T = Timer
- Set Dic = CreateObject("Scripting.Dictionary") '创建一个字典对象
- Set Did = CreateObject("Scripting.Dictionary")
- Dic.Add (ThisWorkbook.Path & ""), ""
- 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
-
- a = 3 '总表数据开始行
- 'a = ThisWorkbook.Sheets(1).Range("a65536").End(xlUp).Row + 1 '如果总表是不断累加的,总表数据开始行
- For Each Ke In Dic.keys '以查找总表所在文件夹下所有excel文件为例
- MyFileName = Dir(Ke & "*.xls") 'EXCEL2003为:*.xlsx,excel2007为:*.xlsx
- Do While MyFileName <> ""
- If MyFileName <> ThisWorkbook.Name Then
- Set xlBook = Workbooks.Open(Ke & MyFileName) '打开已经存在的EXCEL工件簿文件
- For Each sh In xlBook.Worksheets '遍历工作表
- If sh.Cells(5, 2) = "省份" Then '工作表为所需表
- ThisWorkbook.Sheets(1).Cells(a, 1) = sh.Cells(5, 3)
- ThisWorkbook.Sheets(1).Cells(a, 2) = sh.Cells(6, 3)
- ThisWorkbook.Sheets(1).Cells(a, 3) = sh.Cells(5, 9)
- ThisWorkbook.Sheets(1).Cells(a, 4) = sh.Cells(6, 9)
- '..............以此类推
- a = a + 1 '记录指针指向下一行
- End If
- Next
- Windows(MyFileName).Close (False) '关闭工作簿,不保存
- End If
- MyFileName = Dir
- Loop
- Next
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- MsgBox "一共用时:" & Timer - T & "秒"
- End Sub
复制代码 |
|