|
本帖最后由 mjzxlmg 于 2013-4-2 18:06 编辑
[code=vb]Sub Sum()
Dim dic As Object, arr, result(), i&, j&, m&, s&
Set dic = CreateObject("scripting.dictionary") '定义字典
arr = Sheet1.[a1].CurrentRegion.Value '将工作表数据装入数组ARR
ReDim result(1 To UBound(arr), 1 To UBound(arr, 2) - 2) '重新声明结果动态数组result的大小
result(1, 1) = arr(1, 4): result(1, 2) = "月数" '以下将标题写入输出数组result
For j = 5 To UBound(arr, 2)
result(1, j - 2) = arr(1, j)
Next
m = 1
For i = 2 To UBound(arr) '在数组ARR中循环
s = dic(Trim(arr(i, 4)))
If s = Empty Then
m = m + 1
dic(Trim(arr(i, 4))) = m '将姓名作为关键字装入字典dic,累计姓名数量作为对应项
s = m '替换
result(s, 1) = Trim(arr(i, 4)) '输出数组的第一列为姓名
End If
result(s, 2) = result(s, 2) + 1 '输出数组的第二列月份累计数
For j = 5 To UBound(arr, 2)
If Len(arr(i, j)) Then result(s, j - 2) = result(s, j - 2) + arr(i, j) '输出数组的其余列为累加
Next
Next
If m > 1 Then '如果存在关键字
With Sheet3 '结果写入SHEET3
.UsedRange.ClearContents '清空工作表数据
With .[a1].Resize(m, UBound(result, 2))
.Value = result '将数据写入工作表A1开始的区域
.Font.Size = 10 '字号设定为10磅
.Borders.LineStyle = 1 '加入边框
.Columns.AutoFit '自定调整列宽
End With
End With
End If
Set dic = Nothing '释放字典
End Sub[/code]
测试附件:
保险汇总台账.rar
(16.92 KB, 下载次数: 519)
|
|