|
'表格并不规范,,,
Option Explicit
Sub test()
Dim i, j, filename(), m, n, dic, f, arr
If Not getfilename(filename, ThisWorkbook.Path, ".xlsm") Then MsgBox "!": Exit Sub
Set dic = CreateObject("scripting.dictionary")
ReDim brr(1 To 10 ^ 4, 1 To 30): n = 2
For i = 1 To UBound(filename)
With GetObject(filename(i))
arr = .Sheets("sheet1").UsedRange
.Close
End With
f = Split(filename(i), "\")
f = Left(f(UBound(f)), InStrRev(f(UBound(f)), ".") - 1)
If Not dic.exists(arr(1, 2)) Then n = n + 1: dic(arr(1, 2)) = n
For j = 2 To UBound(arr, 1)
m = m + 1: brr(m, 2) = arr(j, 1): brr(m, dic(arr(1, 2))) = arr(j, 2)
If j = 2 Then brr(m, 1) = f
Next j, i
With Sheets("希望得到的结果").[a1]
.Resize(Rows.Count, n + 5).ClearContents
.Offset(, 2).Resize(, dic.Count) = dic.keys
.Offset(1).Resize(m, n) = brr
End With
End Sub
Function getfilename(filename, pth, mark) As Boolean
Dim f, n
If Right(pth, 1) <> "\" Then pth = pth & "\"
f = Dir(pth & "*.*")
Do While Len(f) > 0
If LCase(Right(f, Len(mark))) = LCase(mark) Then
If Left(f, 1) <> "~" And InStr(f, "如何提取数据") = 0 Then
n = n + 1: ReDim Preserve filename(1 To n)
filename(n) = pth & f
End If
End If
f = Dir
Loop
If n > 0 Then getfilename = True
End Function |
|