|
楼主 |
发表于 2017-2-27 02:03
|
显示全部楼层
Sub 汇总()
Dim r&, arr, brr, dic, tmp$, i&, p&, n&, quantity#, weight#, tt#
Dim Cel As Range, EEvents As Boolean
Application.ScreenUpdating = False
r = Sheet1.Cells(Rows.Count, 5).End(xlUp).Row
Sheet1.Range("D5") = "=IF(C5="""",IF(B5="""","""",B5&""-""&C5),B5&""-""&C5)"
Sheet1.Range("D5").Copy Sheet1.Range("D5:D" & r)
If r <= 标题行行数 Then Exit Sub
If MsgBox("是否对计算式强制重算结果?", vbYesNo) = vbYes Then
tt = Timer
EEvents = Application.EnableEvents
Application.EnableEvents = False
On Error Resume Next
For Each Cel In Sheet1.Cells(标题行行数 + 1, 计算式).Resize(r - 标题行行数)
tmp = ExpClean(Cel)
Cel.Offset(, 结果 - 计算式) = tmp
If Err.Number Then
Err.Clear
Cel.Offset(, 结果 - 计算式).Value = "'" & Cel.Offset(, 结果 - 计算式)
End If
Next
Application.EnableEvents = EEvents
Else
tt = Timer
End If
Set dic = CreateObject("Scripting.Dictionary")
arr = Sheet1.Range("A1").Resize(r, 表格总列数)
ReDim brr(0 To r + 1, 1 To 6)
brr(0, 1) = "序号"
brr(0, 2) = "项目名称"
brr(0, 3) = "单位"
brr(0, 4) = "数量"
brr(0, 5) = "理论重量"
brr(0, 6) = "重量t"
For i = 标题行行数 + 1 To r
If Len(arr(i, 计算式)) Then
If dic.exists(arr(i, 项目名称)) Then
p = dic(arr(i, 项目名称))
brr(p, 4) = brr(p, 4) + arr(i, 结果)
brr(p, 6) = brr(p, 6) + arr(i, 重量)
Else
n = n + 1
dic(arr(i, 项目名称)) = n
brr(n, 1) = n
brr(n, 2) = arr(i, 项目名称)
brr(n, 3) = arr(i, 单位)
brr(n, 4) = arr(i, 结果)
brr(n, 5) = arr(i, 理论重量)
brr(n, 6) = arr(i, 重量)
End If
quantity = quantity + arr(i, 结果)
weight = weight + arr(i, 重量)
End If
Next
n = n + 1
brr(n, 1) = "合计"
brr(n, 4) = quantity
brr(n, 6) = weight
With Sheet2.Range("A1").Resize(n + 1, 6)
.EntireColumn.Clear
.Value = brr
.Borders.LineStyle = 1
.Columns(4).NumberFormat = "0.000_ "
.Columns(6).NumberFormat = "0.000_ "
.Columns(1).HorizontalAlignment = xlCenter
.Columns(3).HorizontalAlignment = xlCenter
.Resize(1).HorizontalAlignment = xlCenter
.Resize(1).Font.Bold = True
.EntireColumn.AutoFit
MsgBox Format(Timer - tt, "汇总完成,用时0.00秒!")
.Parent.Select
End With
End Sub |
|