|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Option Explicit
Sub test()
Dim ar, br, vResult, i&, j&, k&, r&, n&, p&, strFileName$, strPath$, strName$
Application.ScreenUpdating = False
ReDim vResult(1 To 10 ^ 5, 1 To 5)
strPath = ThisWorkbook.Path & "\"
strFileName = Dir(strPath & "*.xls")
Do Until strFileName = ""
If strFileName <> ThisWorkbook.Name Then
With GetObject(strPath & strFileName)
strName = Left(strFileName, InStrRev(strFileName, ".") - 1)
With .Worksheets(2)
n = .Cells(.Rows.Count, "F").End(xlUp).Row
With .Range("F1:I" & n)
.Parent.Sort.SortFields.Clear
.Sort key1:=.Item(2), Order1:=xlAscending, Header:=xlNo
ar = .Resize(.Rows.Count + 1).Value
p = 1
For i = 1 To UBound(ar) - 1
If ar(i + 1, 4) <> ar(p, 4) Then
If i - p > 0 Then
br = Range(.Cells(p, 1), .Cells(i, 4)).Value
For k = 1 To UBound(br)
r = r + 1
For j = 1 To UBound(br, 2)
vResult(r, j) = br(k, j)
Next j
vResult(r, 5) = strName
Next k
End If
p = i + 1
End If
Next i
End With
End With
.Close False
End With
End If
strFileName = Dir
Loop
If r Then
[A1].CurrentRegion.Clear
[A1].Resize(r, UBound(vResult, 2)) = vResult
End If
Application.ScreenUpdating = True
Beep
End Sub
|
|