|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub BJF() 'by: bajifeng
- Dim brr(), crr(), Rng As Range
- Set sho = ThisWorkbook.ActiveSheet
- fl = ListFile(ThisWorkbook.Path, True, "*.xls?") 'ThisWorkbook.Path是当前代码文件所在路径,路径名可以根据需求修改
- 'Debug.Print UBound(fl)
- Application.ScreenUpdating = False '关闭屏幕刷新,提高速度
- For l = 0 To UBound(fl)
- If fl(l) <> "" Then
- If InStr(fl(l), "汇总") = 0 Then
- On Error GoTo 100
- 'Debug.Print fl(l)
- Workbooks.Open fl(l)
- On Error GoTo 0
- '+++++++++++++++++++++++++++++++++++++++
- For Each sht In Worksheets
- n = n + 1
- arr = sht.[o1:s1]
- ReDim Preserve brr(1 To 5, 1 To n)
- 'Debug.Print TypeName(ds)
- For i = 1 To n
- brr(1, n) = arr(1, 1)
- brr(2, n) = arr(1, 2)
- brr(3, n) = arr(1, 3)
- brr(4, n) = arr(1, 4)
- brr(5, n) = arr(1, 5)
- Next
- Next
- '+++++++++++++++++++++++++++++++++++++++
- If fl(l) <> ThisWorkbook.Path Then ActiveWorkbook.Close True
- End If
- End If
- 100:
- Next
- [a2:f999] = ""
- [a2].Resize(n, 5) = Application.Transpose(brr)
- Columns("a:f").AutoFit
- Application.ScreenUpdating = True
- MsgBox "处理完毕!"
- End Sub
- Private Function ListFile(MuLu As String, Zi As Boolean, Optional LeiXing As String = "")
- Dim MyFile As String, ms As String
- Dim arr, brr, x
- Dim i As Integer
- Set d = CreateObject("Scripting.Dictionary")
- If Left(MuLu, 1) <> "" Then MuLu = MuLu & ""
- d.Add MuLu, ""
- i = 0
- Do While i < d.Count
- brr = d.keys
- MyFile = Dir(brr(i), vbDirectory)
- Do While MyFile <> ""
- If MyFile <> "." And MyFile <> ".." Then
- If (GetAttr(brr(i) & MyFile) And vbDirectory) = vbDirectory Then d.Add (brr(i) & MyFile & ""), ""
- End If
- MyFile = Dir
- Loop
- If Zi = False Then Exit Do
- i = i + 1
- Loop
- If LeiXing = "" Then
- ListFile = Application.Transpose(d.keys)
- Else
- For Each x In d.keys
- MyFile = Dir(x & LeiXing)
- Do While MyFile <> ""
- ms = ms & x & MyFile & ","
- MyFile = Dir
- Loop
- If Zi = False Then Exit For
- Next
- If ms = "" Then ms = "没有符合要求的文件,"
- 'ListFile = Application.Transpose(Split(ms, ",")) '下标从1开始
- ListFile = Split(ms, ",") '下标从0开始
- End If
- End Function
复制代码 |
|