|
'修改了一下,再试试
Option Explicit
Sub test()
Dim arr, i, j, k, m, sum, brr, title, dic
Call doevent(False)
Set dic = CreateObject("scripting.dictionary")
With Sheets("明细表")
arr = .[a5].CurrentRegion.Offset(1)
title = .Range("a4").Resize(, UBound(arr, 2))
End With
brr = arr
For i = 1 To UBound(arr, 1) - 1: dic(arr(i, 1)) = vbNullString: Next
For Each i In Sheets
If i.Name <> "明细表" Then Sheets(i.Name).Delete
Next
For Each i In dic.keys
If i <> "明细表" Then
Sheets.Add
ActiveSheet.Name = i
With [a4].Resize(, UBound(title, 2))
.Value = title
.Borders.LineStyle = xlContinuous
End With
End If
Next
For i = 1 To UBound(arr, 1) - 1
If Len(arr(i, 9)) = 0 Then
m = m + 1: sum = sum + arr(i, 7)
For j = 1 To UBound(arr, 2): brr(m, j) = arr(i, j): Next
End If
If arr(i + 1, 1) <> arr(i, 1) Then
For j = 1 To UBound(arr, 1) - 1
If InStr(arr(j, 9), arr(i, 1)) Then
m = m + 1: sum = sum + arr(j, 7)
For k = 1 To UBound(arr, 2): brr(m, k) = arr(j, k): Next
End If
Next
With Sheets(arr(i, 1)).[a5]
If m > 0 Then
With .Resize(m, UBound(brr, 2))
.Value = brr
.Borders.LineStyle = xlContinuous
End With
.Cells(m + 2, 1) = "合计"
.Cells(m + 2, 7) = sum
End If
End With
m = 0: sum = 0
End If
Next
Call doevent(True)
End Sub
Function doevent(flag)
With Application
.DisplayAlerts = flag
.ScreenUpdating = flag
End With
End Function |
|