|
本帖最后由 hzruziniu 于 2016-10-28 17:47 编辑
Sub 库存汇总2()
ActiveSheet.Unprotect
Dim arr, brr(), i&, j&, r&, aa, bb
Dim d, k, t, x
Set d = CreateObject("Scripting.Dictionary")
Sheet3.Activate
arr = [a1].CurrentRegion
For i = 3 To UBound(arr)
x = arr(i, 3) & "," & arr(i, 4)
d(x) = d(x) & i & ","
Next
k = d.keys: t = d.items
ReDim brr(1 To d.Count, 1 To 9)
For i = 0 To UBound(k)
bb = Split(k(i), ",")
t(i) = Left(t(i), Len(t(i)) - 1)
If InStr(t(i), ",") Then
aa = Split(t(i), ",")
brr(i + 1, 1) = arr(aa(0), 1): brr(i + 1, 2) = arr(aa(0), 2): brr(i + 1, 5) = arr(aa(0), 5): brr(i + 1, 6) = arr(aa(0), 6)
brr(i + 1, 3) = bb(0): brr(i + 1, 4) = bb(1) ': Brr(i + 1, 8) = bb(2)
For j = 0 To UBound(aa)
brr(i + 1, 7) = brr(i + 1, 7) + arr(aa(j), 7)
brr(i + 1, 9) = brr(i + 1, 9) + arr(aa(j), 9)
Next
Else
brr(i + 1, 1) = arr(t(i), 1): brr(i + 1, 2) = arr(t(i), 2): brr(i + 1, 5) = arr(t(i), 5): brr(i + 1, 6) = arr(t(i), 6)
brr(i + 1, 3) = bb(0): brr(i + 1, 4) = bb(1) ': Brr(i + 1, 8) = bb(2)
brr(i + 1, 7) = arr(t(i), 7)
brr(i + 1, 9) = arr(t(i), 9)
End If
Next
Sheet7.Activate
[a3:i5000].ClearContents '清除内容
[a3:i5000].Borders.LineStyle = xlNone '删除边框 线'
[a3].Resize(UBound(brr), 9) = brr 'brr赋值
r = Range("a65536").End(3).Row
For i = 3 To r
If Range("i" & i) <> 0 And Range("g" & i) <> 0 Then
Range("h" & i).Value = Range("i" & i).Value / Range("g" & i).Value '单价列计算
End If
Next
'==============================按升序排序,先排C列再排D列
Sheet7.Range("a3:i" & r).Select
With Sheet7.Sort
.SortFields.Add Key:=Range("C2:C" & r), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("D3:D" & r), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range("A3:I" & r)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
With Range("a3:i" & r) '
.Borders.LineStyle = xlContinuous '添加边框线
.BorderAround xlContinuous, xlMedium '外边框加粗
End With
For i = r To 3 Step -1
If Cells(i, 7) = 0 Then Rows(i).Delete '删除0值行
Next
End Sub
|
|