|
我用上面的附件测试没有超过10s的
下面三个方法都是排序法,你可以测一下速度:
Sub Macro1() '排序复制工作表法
tt = Timer
Dim arr, brr, sh As Worksheet, MyPath$, i&, lr&, m&, s$, a As Shape
Set sh = ActiveSheet
lr = Range("A65535").End(xlUp).Row
With Range("A5:AA" & lr)
.Sort Key1:=[b5].Resize(lr - 4), Order1:=xlAscending
arr = .Value
End With
ReDim brr(1 To UBound(arr), 1)
For i = 1 To lr - 4
If arr(i, 2) <> s Then
m = m + 1
brr(m, 0) = arr(i, 2)
brr(m, 1) = i + 4
s = arr(i, 2)
End If
Next
brr(m + 1, 1) = i + 4
MyPath = ThisWorkbook.Path & "\"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For i = 1 To m
sh.Copy
With ActiveSheet
.UsedRange.Offset(4).Clear
sh.Cells(brr(i, 1), 1).Resize(brr(i + 1, 1) - brr(i, 1), 27).Copy .[a5]
For Each a In .Shapes
a.Delete
Next
End With
ActiveWorkbook.SaveAs MyPath & brr(i, 0) & ".xls"
ActiveWorkbook.Close
Next
Application.ScreenUpdating = True
MsgBox Timer - tt
End Sub
Sub Macro2() '排序新建工作簿数组法
tt = Timer
Dim arr, brr, sh As Worksheet, MyPath$, i&, lr&, m&, s$, rng As Range
Set rng = Range("A1:AA4")
Set sh = ActiveSheet
lr = Range("A65535").End(xlUp).Row
With Range("A5:AA" & lr)
.Sort Key1:=[b5].Resize(lr - 4), Order1:=xlAscending
arr = .Value
End With
ReDim brr(1 To UBound(arr), 1)
For i = 1 To lr - 4
If arr(i, 2) <> s Then
m = m + 1
brr(m, 0) = arr(i, 2)
brr(m, 1) = i + 4
s = arr(i, 2)
End If
Next
brr(m + 1, 1) = i + 4
MyPath = ThisWorkbook.Path & "\"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For i = 1 To m
With Workbooks.Add(xlWBATWorksheet)
With .Sheets(1)
rng.Copy .[a1]
arr = sh.Cells(brr(i, 1), 1).Resize(brr(i + 1, 1) - brr(i, 1), 27)
.[a5].Resize(UBound(arr), 27) = arr
End With
.SaveAs Filename:=MyPath & brr(i, 0) & ".xls"
.Close
End With
Next
Application.ScreenUpdating = True
MsgBox Timer - tt
End Sub
Sub Macro3() '排序新建工作簿复制区域法
tt = Timer
Dim arr, brr, sh As Worksheet, MyPath$, i&, lr&, m&, s$, rng As Range
Set rng = Range("A1:AA4")
Set sh = ActiveSheet
lr = Range("A65535").End(xlUp).Row
With Range("A5:AA" & lr)
.Sort Key1:=[b5].Resize(lr - 4), Order1:=xlAscending
arr = .Value
End With
ReDim brr(1 To UBound(arr), 1)
For i = 1 To lr - 4
If arr(i, 2) <> s Then
m = m + 1
brr(m, 0) = arr(i, 2)
brr(m, 1) = i + 4
s = arr(i, 2)
End If
Next
brr(m + 1, 1) = i + 4
MyPath = ThisWorkbook.Path & "\"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For i = 1 To m
With Workbooks.Add(xlWBATWorksheet)
With .Sheets(1)
rng.Copy .[a1]
sh.Cells(brr(i, 1), 1).Resize(brr(i + 1, 1) - brr(i, 1), 27).Copy .[a5]
End With
.SaveAs Filename:=MyPath & brr(i, 0) & ".xls"
.Close
End With
Next
Application.ScreenUpdating = True
MsgBox Timer - tt
End Sub |
|