|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
有点难度
请注意:每个数据区以“Location”开头,“Average”结束,日期就在第三列(没有设变量)
Sub Macro1()
Dim MyPath$, MyName$, ary(), arr, brr(), crr(), i&, j&, m&, n&, lr&
Dim c As Range, d As Object, ds As Object, firstAddress$
Set d = CreateObject("scripting.dictionary")
Set ds = CreateObject("scripting.dictionary")
arr = Range("a1:c" & [a65536].End(xlUp).Row)
lr = UBound(arr)
ReDim brr(1 To lr)
With [a:a]
Set c = .Find("Location", , , xlWhole)
If Not c Is Nothing Then
firstAddress = c.Address
Do
i = c.Row
n = n + 1
ReDim Preserve ary(1 To n)
ary(n) = i
d(arr(i + 1, 1)) = n
ReDim crr(1 To WorksheetFunction.Match("Average", c.Offset(1).Resize(lr - i), 0) - 1, 1 To 3)
brr(n) = crr
m = 0
For j = i + 1 To i + UBound(crr)
m = m + 1
If Len(arr(j, 1) & arr(j, 3)) Then ds(arr(j, 1) & arr(j, 3)) = m
Next
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
MyPath = ThisWorkbook.Path & "\"
MyName = Dir(MyPath & "*.xlsx")
Application.ScreenUpdating = False
Do While MyName <> ""
With GetObject(MyPath & MyName)
arr = .Sheets(1).Range("A1").CurrentRegion
.Close False
End With
For i = 2 To UBound(arr)
If d.Exists(arr(i, 1)) Then
If ds.Exists(arr(i, 1) & arr(i, 2)) Then
For j = 3 To 5
brr(d(arr(i, 1)))(ds(arr(i, 1) & arr(i, 2)), j - 2) = arr(i, j)
Next
End If
End If
Next
MyName = Dir
Loop
For i = 1 To n
Cells(ary(i) + 1, 4).Resize(UBound(brr(i)), 3) = brr(i)
Next
Application.ScreenUpdating = True
MsgBox "finished"
End Sub |
|