|
发表于 2018-8-28 17:46
来自手机
|
显示全部楼层
Sub test2()
Dim arr1, arr2, dic
Dim i&, j&, k&, l&, sh As Worksheet, start As Double
Application.ScreenUpdating = False
start = Timer
Set dic = CreateObject("scripting.dictionary")
arr1 = Range("A4").CurrentRegion
j = 1
For i = 5 To UBound(arr1)
If Not dic.exists(arr1(i, 3)) And arr1(i, 3) <> "" Then
dic(arr1(i, 3)) = arr1(i, 3)
Set sh = Worksheets.Add(after:=ActiveSheet)
sh.Name = arr1(i, 3)
Worksheets(arr1(i, 3)).Range("A1:K1") = Application.Index(arr1, 1, 0)
Worksheets(arr1(i, 3)).Range("A2:K2") = Application.Index(arr1, 2, 0)
Worksheets(arr1(i, 3)).Range("A3:K3") = Application.Index(arr1, 3, 0)
Worksheets(arr1(i, 3)).Range("A4:K4") = Application.Index(arr1, 4, 0)
ActiveSheet.Range("A5:K5") = Application.Index(arr1, i, 0)
Else
If arr1(i, 3) <> "" Then
Worksheets(arr1(i, 3)).Range("A" & Range("A1048576").End(xlUp).Row + 1).Resize(1, 11).Value = Application.Index(arr1, i, 0)
End If
End If
Next
Worksheets(1).Range("A5").CurrentRegion.Copy
For Each sh In Worksheets
If sh.Index <> 1 Then
l = sh.Range("A1048576").End(xlUp).Row + 1
sh.Range("A" & l) = "合计"
sh.Range("D" & l) = Application.WorksheetFunction.Sum(sh.Range("D5:D" & l - 1))
sh.Range("E" & l) = Application.WorksheetFunction.Sum(sh.Range("E5:E" & l - 1))
sh.Range("F" & l) = Application.WorksheetFunction.Sum(sh.Range("F5:F" & l - 1))
sh.Range("G" & l) = Application.WorksheetFunction.Sum(sh.Range("G5:G" & l - 1))
sh.Range("H" & l) = Application.WorksheetFunction.Sum(sh.Range("H5:H" & l - 1))
sh.Range("I" & l) = Application.WorksheetFunction.Sum(sh.Range("I5:I" & l - 1))
sh.Range("J" & l) = Application.WorksheetFunction.Sum(sh.Range("J5:J" & l - 1))
sh.UsedRange.PasteSpecial xlPasteFormats
End If
Next
Application.ScreenUpdating = True
MsgBox "用时:" & Format(Timer - start, "0.00") & "秒。"
End Sub |
|