|
发表于 2014-2-28 19:01
|
显示全部楼层
本楼为最佳答案
Sub Macro1()
Dim wb As Workbook, rng As Range
Dim x&, y&, mypath$, myfile$, s&, s2&
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.StatusBar = "正在汇总………"
ActiveSheet.UsedRange.Clear
mypath = ThisWorkbook.Path & "\"
myfile = Dir(mypath & "*.xls")
Do While myfile <> ""
If myfile <> ThisWorkbook.Name Then
Set wb = GetObject(mypath & myfile)
x = Range("j65536").End(xlUp).Row
n = wb.Sheets(1).Range("l65536").End(xlUp).Row
x2 = IIf(x = 1, 1, x + 1)
wb.Sheets(1).Range("a5:l" & n).Copy Cells(x2, 1)
Cells(x2, "m").Resize(n - 4, 1) = wb.Name
wb.Close 0
End If
myfile = Dir
Loop
ActiveSheet.UsedRange.UnMerge
y = ActiveSheet.UsedRange.Rows.Count
[b1].Resize(y, 1).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
ActiveSheet.UsedRange.Sort Key1:=[b1], Order1:=xlAscending, Header:=xlGuess
Set rng = [b1]: [a1] = 1
s2 = 1: [c1] = 1
For i = 2 To y + 1
If Cells(i, 2) = Cells(i - 1, 2) Then
s2 = s2 + 1
Cells(i, 3) = s2
Set rng = Union(rng, Cells(i, 2))
Else
s2 = 1: s = s + 1
rng.Offset(, -1).Merge
rng.Merge
Set rng = Cells(i, 2)
Cells(i, 3) = s2
rng.Offset(, -1) = s + 1
End If
Next
Cells(y + 1, 1) = ""
Cells(y + 1, 3) = ""
[1:4].Insert Shift:=xlDown
Set wb = GetObject(mypath & "北京.xls")
wb.Sheets(1).Range("A1:L4").Copy [a1]
wb.Close 0
Application.StatusBar = "已完成"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
|
|