|
这样???????????
- Sub GetData()
- Call Sum
- Dim arr, brr, crr, fileArr, d As Object
- Dim sht As Worksheet, sh As Worksheet, wd As Workbook
- Dim rng As Range
- Dim thePath$, i&, j&, n
- Set sh = ActiveSheet
- sh.UsedRange.Offset(1).ClearContents
- sh.Range("A1:C1") = Array("文件夹名", "工作簿名", "工作表名")
- arr = Range("A1").CurrentRegion
- Set d = CreateObject("Scripting.Dictionary")
- For i = 1 To UBound(arr, 2)
- d(arr(1, i)) = i
- Next
- With Application.FileDialog(msoFileDialogFolderPicker)
- .InitialFileName = ThisWorkbook.Path & ""
- If .Show = False Then Exit Sub
- thePath = .SelectedItems(1) & ""
- End With
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Application.Calculation = xlManual
- If Right(thePath, 1) <> "" Then thePath = thePath & ""
- ReDim brr(1 To 90000, 1 To UBound(arr, 2))
- fileArr = GetName(thePath)
- For k = 0 To UBound(fileArr)
- Set wb = Workbooks.Open(fileArr(k))
- For Each sht In wb.Worksheets
- Set rng = sht.Cells.Find("*")
- If Not rng Is Nothing Then
- r = sht.Cells.Find("*", , , , xlByRows, xlPrevious).Row
- c = sht.Cells.Find("*", , , , xlByColumns, xlPrevious).Column
- crr = sht.Range(sht.Cells.Find("*", sht.Cells(Rows.Count, Columns.Count), , , xlByRows, xlNext), sht.Cells(r, c))
- For i = 2 To UBound(crr)
- x = x + 1
- For j = 1 To UBound(crr, 2)
- n = d(crr(1, j))
- If n <> "" Then
- m = 1
- brr(x, 1) = Mid(wb.Path, InStrRev(wb.Path, "") + 1)
- If Right(wb.Name, 4) = ".xls" Then brr(x, 2) = Left(wb.Name, Len(wb.Name) - 4) Else brr(x, 2) = Left(wb.Name, Len(wb.Name) - 5) '自动适用版本!!!
- brr(x, 3) = sht.Name
- brr(x, n) = crr(i, j)
- End If
- Next
- If m = 0 Then x = x - 1
- m = 0
- Next
- End If
- Next
- wb.Close False
- Next
- sh.Range("A2").Resize(x, UBound(arr, 2)) = brr
- Application.ScreenUpdating = True
- Application.Calculation = xlAutomatic
- Application.DisplayAlerts = True
- MsgBox "OK!"
- End Sub
- Sub Sum()
- Dim sh As Worksheet, arr, d As Object, i&, j&, MyPath$, Filepath, r&, c&
- Cells.ClearContents
- With Application.FileDialog(msoFileDialogFolderPicker)
- .InitialFileName = ThisWorkbook.Path & ""
- If .Show = False Then Exit Sub
- MyPath = .SelectedItems(1) & ""
- End With
- Application.ScreenUpdating = False
- Set d = CreateObject("Scripting.Dictionary")
- Filepath = GetName(MyPath)
- For kk = 0 To UBound(Filepath)
- Set wb = Workbooks.Open(Filepath(kk))
- For Each sh In wb.Sheets
- If Application.CountA(sh.UsedRange) Then
- r = sh.Cells.Find("*", , , , xlByRows, xlPrevious).Row
- c = sh.Cells.Find("*", , , , xlByColumns, xlPrevious).Column
- arr = sh.Range(sh.Cells.Find("*", sh.Cells(Rows.Count, Columns.Count), , , xlByRows, xlNext), sh.Cells(r, c))
- For j = 1 To UBound(arr, 2)
- If Len(arr(1, j)) Then
- If Not d.Exists(arr(1, j)) Then d(arr(1, j)) = ""
- End If
- Next
- End If
- Next
- wb.Close
- Next
- Range("D1").Resize(, d.Count) = d.Keys
- Application.ScreenUpdating = True
- End Sub
- Function GetName(lj As String)
- Dim MyName, dic, Did, i, t, F, tt, MyFileName
- Set dic = CreateObject("Scripting.Dictionary")
- Set Did = CreateObject("Scripting.Dictionary")
- dic.Add (lj), ""
- 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
- For Each Ke In dic.Keys
- MyFileName = Dir(Ke & "*.xls*")
- Do While MyFileName <> ""
- If MyFileName <> ThisWorkbook.Name Then Did.Add (Ke & MyFileName), ""
- MyFileName = Dir
- Loop
- Next
- GetName = Did.Keys
- End Function
复制代码
|
|