|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 合并统计()
Dim i, j, k, m, t, n, s, a, b, x, irow As Integer
Dim kk
Dim ar, br, cr As Variant
Dim d1, d2 As Object
Set d1 = CreateObject("scripting.dictionary")
Set d2 = CreateObject("scripting.dictionary")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
irow = Sheets("数据").[a65536].End(xlUp).Row
ar = Sheets("数据").[a1].CurrentRegion
d1(ar(2, 1)) = d1(ar(2, 1)) + 1
d2(ar(2, 2) & "," & ar(2, 3) & "," & ar(2, 4) & "," & ar(2, 1)) = ar(2, 4)
m = ar(2, 4)
t = 1
For i = 3 To irow
d1(ar(i, 1)) = d1(ar(i, 1)) + 1
If ar(i, 4) = 20 And ar(i - 1, 4) = ar(i, 4) And ar(i - 1, 2) = ar(i, 2) And ar(i - 1, 3) = ar(i, 3) Then
m = m + ar(i, 4)
d2.Remove ar(i - 1, 2) & "," & ar(i - 1, 3) & "," & ar(i - 1, 4) & "," & ar(i - 1, 1)
d2(ar(i, 2) & "," & ar(i, 3) & "," & ar(i, 4) & "," & ar(i, 1)) = t & "—" & m / 20 + t - 1
Else
m = ar(i, 4): t = ar(i, 1)
d2(ar(i, 2) & "," & ar(i, 3) & "," & ar(i, 4) & "," & ar(i, 1)) = t
End If
Next
ReDim br(1 To d2.Count, 1 To 4)
For Each kk In d2.keys
n = n + 1
br(n, 1) = d2(kk)
For s = 1 To 2
br(n, s + 1) = Split(kk, ",")(s - 1)
Next
If Val(Split(kk, ",")(2)) = 20 Then
a = WorksheetFunction.Find("—", br(n, 1))
x = Len(br(n, 1))
br(n, 4) = CStr(20 & "*" & Val(Right(br(n, 1), x - a)) - Val(Left(br(n, 1), a - 1)) + 1)
Else
br(n, 4) = Val(Split(kk, ",")(2))
End If
Next
ReDim cr(1 To d2.Count, 1 To 4)
With Sheets("数据")
.[p2].Resize(100, 4).Clear
.[p2] = "盒子号": .[q2] = "规格": .[r2] = "度数": .[s2] = "数量"
.[p3].Resize(d2.Count, 4) = br
For b = 3 To d2.Count + 2
If d1(.Cells(b, 16).Value) > 1 Then
.Cells(b, 16).Resize(d1(.Cells(b, 16).Value), 1).Merge
End If
Next
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "ok"
End Sub |
|