|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 按客户拆分()
Application.ScreenUpdating = False
Dim ar As Variant
Dim i As Long, r As Long, rs As Long
Dim br(), brr()
Dim d As Object
Set d = CreateObject("scripting.dictionary")
Set dc = CreateObject("scripting.dictionary")
Set dic = CreateObject("scripting.dictionary")
Application.DisplayAlerts = False
For Each sh In Sheets
If sh.Index > 3 Then
sh.Delete
End If
Next sh
Application.DisplayAlerts = True
With Sheets("客户期初")
rs = .Cells(Rows.Count, 1).End(xlUp).Row
cr = .Range("a4:f" & rs)
End With
For i = 2 To UBound(cr)
If Trim(cr(i, 2)) <> "" Then
dc(Trim(cr(i, 2))) = i
End If
Next i
With Sheets("汇总表")
r = .Cells(Rows.Count, 16).End(xlUp).Row
y = .Cells(4, Columns.Count).End(xlToLeft).Column
If r < 7 Then MsgBox "汇总表为空!": End
ar = .Range(.Cells(5, 1), .Cells(r, 16))
End With
For i = 3 To UBound(ar)
If Trim(ar(i, 16)) <> "" Then
d(Trim(ar(i, 16))) = ""
End If
Next i
For Each k In d.keys
n = 0: dic.RemoveAll
ReDim br(1 To UBound(ar), 1 To 12)
n = 1
br(n, 5) = "上年结存"
xh = dc(k)
br(n, 12) = cr(xh, 6)
For i = 3 To UBound(ar)
If Trim(ar(i, 16)) = k Then
n = n + 1
zd = Format(ar(i, 1), "yyyym")
dic(zd) = ""
br(n, 1) = ar(i, 1)
For j = 4 To 11
br(n, j) = ar(i, j)
Next j
br(n, 12) = br(n - 1, 12) + br(n, 10) + br(n, 11)
End If
Next i
Sheets("模板").Copy after:=Sheets(Sheets.Count)
With ActiveSheet
.Name = k
.[e4] = k
xh = dc(k)
If xh <> "" Then
.[d4] = cr(xh, 1)
.[h4] = cr(xh, 3)
.[l4] = cr(xh, 5)
.[j4] = cr(xh, 4)
.[l7] = cr(xh, 6)
End If
For Each kc In dic.keys
m = 0
ReDim brr(1 To n, 1 To 12)
For i = 2 To n
zd = Format(br(i, 1), "yyyym")
If zd = kc Then
m = m + 1
For j = 1 To UBound(br, 2)
brr(m, j) = br(i, j)
Next j
End If
Next i
.[a7].Resize(1, UBound(br, 2)) = br
ws = .Cells(Rows.Count, 5).End(xlUp).Row + 1
.Cells(ws, 1).Resize(m, UBound(brr, 2)) = brr
.Cells(ws + m, 5) = "本月合计"
.Cells(ws + m, 8) = Application.Sum(.Range("h" & ws & ":h" & ws + m - 1))
.Cells(ws + m, 10) = Application.Sum(.Range("j" & ws & ":j" & ws + m - 1))
.Cells(ws + m, 11) = Application.Sum(.Range("k" & ws & ":k" & ws + m - 1))
.Cells(ws + m, 12) = .Cells(ws + m - 1, 12)
.Rows(ws + m).Font.ColorIndex = 3
Next kc
ws = .Cells(Rows.Count, 5).End(xlUp).Row + 1
.Cells(ws, 5) = "本年合计"
.Rows(ws).Font.ColorIndex = 3
rr = Array(8, 10, 11, 12)
For s = 0 To UBound(rr)
hj = 0
lh = rr(s)
For i = 3 To ws - 1
If Trim(.Cells(i, 5)) = "本月小计" Then
hj = hj + .Cells(i, lh)
End If
Next i
.Cells(ws, lh) = hj
Next s
If ws + 1 <= 91 Then .Rows(ws + 1 & ":91").Delete
End With
Next k
Set d = Nothing
Set dc = Nothing
Set dic = Nothing
Application.ScreenUpdating = True
MsgBox "ok!"
End Sub
|
|