|
Sub 汇总()
Application.ScreenUpdating = False
Dim ar As Variant
Dim i As Long, r As Long
Dim br(), brr()
Dim d As Object
Set sht = ThisWorkbook.Worksheets("模板")
Set d = CreateObject("scripting.dictionary")
ReDim br(1 To 100000, 1 To 6)
For Each sh In Sheets
If sh.Name <> "模板" Then
r = sh.Cells(Rows.Count, 2).End(xlUp).Row
ar = sh.Range("a1:f" & r)
For i = 5 To UBound(ar)
If Trim(ar(i, 2)) <> "" Then
n = n + 1
br(n, 1) = ar(i, 2)
br(n, 2) = ar(i, 3)
br(n, 3) = Replace(ar(3, 1), "零售户名称:", "")
br(n, 4) = Replace(ar(3, 4), "客户代码:", "")
br(n, 5) = Replace(ar(2, 4), "客户经理:", "")
br(n, 6) = Replace(ar(2, 1), "物资名称:", "")
zd = br(n, 5) & "-" & br(n, 6)
d(zd) = ""
End If
Next i
End If
Next sh
If n = "" Then End
For Each k In d.keys
m = 0: s = s + 1
ReDim brr(1 To n, 1 To 4)
For i = 1 To n
zd = br(i, 5) & "-" & br(i, 6)
If zd = k Then
m = m + 1
For j = 1 To 4
brr(m, j) = br(i, j)
Next j
End If
Next i
If s = 1 Then
sht.Copy
Set wb = ActiveWorkbook
With wb.Worksheets(1)
.Name = k
.[f2] = "物资名称:" & Split(k, "-")(0)
.[a2] = "客户经理:" & Split(k, "-")(0)
.[d5].Resize(m, UBound(brr, 2)) = brr
End With
Else
sht.Copy after:=wb.Worksheets(wb.Worksheets.Count)
With wb.ActiveSheet
.Name = k
.[f2] = "物资名称:" & Split(k, "-")(0)
.[a2] = "客户经理:" & Split(k, "-")(0)
.[d5].Resize(m, UBound(brr, 2)) = brr
End With
End If
Next k
wb.SaveAs Filename:=ThisWorkbook.Path & "\发放物资汇总台账" & Format(Date, "yyyymmdd") & ".xlsx"
Set d = Nothing
Application.ScreenUpdating = True
MsgBox "ok!"
End Sub
|
|