ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E文精选 > Excel VBA > 简单调整代码,实现汇总结果的四种效果

简单调整代码,实现汇总结果的四种效果

作者:绿色风 分类: 时间:2022-08-17 浏览:112
楼主
kevinchengcw

当我们获取了一个结果后,如何才能让结果以不同的方式显示出来,从而满足不同需求呢,请看下面例子:

简单的调整代码,实现四种不同的汇总效果




 


代码一:

  1. Sub Sums8_1()
  2.     Dim Dic As Object
  3.     Dim irow%
  4.     Set Dic = CreateObject("Scripting.Dictionary")  '建立字典,利用字典的key值唯一性得到人员清单
  5.     For irow = 2 To [D65536].End(3).Row
  6.         If Not Dic.exists(Cells(irow, 4).Value) Then  '如果不存在该人名,则
  7.             Dic(Cells(irow, 4).Value) = Cells(irow, 6)  '向字典中添加该人名
  8.         Else                                                         '否则(即已存在当前人名时)
  9.             Dic(Cells(irow, 4).Value) = Cells(irow, 6) + Dic(Cells(irow, 4).Value)   'item值等于item值加上对应的销售量
  10.         End If
  11.     Next

  12.     For irow = [D65536].End(3).Row To 2 Step -1  '从后向前插入汇总值可以避免行数计数出错
  13.         If Dic.exists(Cells(irow, 4).Value) Then  '因为是要将汇总值加在个人单项值的后面,所以判断当前单元格对应人名在字典中是否存在,如果存在则
  14.             Rows(irow + 1).Insert 3   '在其下一行插入一空行,并写入下述内容及格式
  15.             Cells(irow + 1, 4) = Cells(irow, 4) & "小计"
  16.             Cells(irow + 1, 7) = Dic(Cells(irow, 4).Value)
  17.             Range(Cells(irow + 1, 4), Cells(irow + 1, 7)).Interior.Color = vbRed
  18.             Dic.Remove (Cells(irow, 4).Value)  '完成后删除该人名,这样不会重复插入
  19.         End If
  20.     Next
  21.     Dic.RemoveAll  ‘清空字典
  22. End Sub


 

代码二:


  1. Sub Sums8_2()
  2.     Dim Dic As Object
  3.     Dim irow%

  4.     Set Dic = CreateObject("Scripting.Dictionary")
  5.     For irow = 2 To [D65536].End(3).Row
  6.         If Not Dic.exists(Cells(irow, 4).Value) Then
  7.             Dic(Cells(irow, 4).Value) = Cells(irow, 6)
  8.         Else
  9.             Dic(Cells(irow, 4).Value) = Cells(irow, 6) + Dic(Cells(irow, 4).Value)
  10.         End If
  11.     Next
  12.    
  13.     i = [D65536].End(3).Row + 1   ‘放在结尾处则最开始行是现有最后一行的下一行
  14.         For irow = 2 To [D65536].End(3).Row
  15.         If Dic.exists(Cells(irow, 4).Value) Then   ‘依序调出各个人的名字
  16.             Cells(i, 4) = Cells(irow, 4).Value & "小计"
  17.             Cells(i, 7) = Dic(Cells(irow, 4).Value)  ‘写入相应值
  18.             Range(Cells(i, 4), Cells(i, 7)).Interior.Color = vbRed
  19.             i = i + 1
  20.             Dic.Remove (Cells(irow, 4).Value)  ‘删除该名字的项,这样就不会重复写入
  21.         End If
  22.     Next

  23.     Dic.RemoveAll
  24. End Sub


 

代码三:


  1. Sub Sums8_3()
  2.     Dim Dic As Object
  3.     Dim irow%

  4.     Set Dic = CreateObject("Scripting.Dictionary")
  5.     For irow = 2 To [D65536].End(3).Row
  6.         If Not Dic.exists(Cells(irow, 4).Value) Then
  7.             Dic(Cells(irow, 4).Value) = Cells(irow, 6)
  8.         Else
  9.             Dic(Cells(irow, 4).Value) = Cells(irow, 6) + Dic(Cells(irow, 4).Value)
  10.         End If
  11.     Next

  12.     For irow = [D65536].End(3).Row To 2 Step -1  ‘类似代码一,要插入行来完成,所以要从后向前可以避免计数出错
  13.         If Dic.exists(Cells(irow, 4).Value) And Cells(irow - 1, 4).Value <> Cells(irow, 4).Value Then  ‘因为是在两个名字交界处,所以判断当前行与其上一行是否一致,不一致则证明是两个名字交界处,在当前行插入并写入数据
  14.             Rows(irow).Insert 3
  15.             Cells(irow, 4) = Cells(irow + 1, 4) & "小计"
  16.             Cells(irow, 7) = Dic(Cells(irow + 1, 4).Value)
  17.             Range(Cells(irow, 4), Cells(irow, 7)).Interior.Color = vbRed
  18.             Dic.Remove (Cells(irow + 1, 4).Value)  ‘移除字典中该名字的内容,防止重复
  19.         End If
  20.     Next
  21.     Dic.RemoveAll
  22. End Sub


 

代码四:


  1. Sub Sums8_4()
  2.     Dim Dic As Object
  3.     Dim irow%

  4.     Set Dic = CreateObject("Scripting.Dictionary")
  5.     For irow = 2 To [D65536].End(3).Row
  6.         If Not Dic.exists(Cells(irow, 4).Value) Then
  7.             Dic(Cells(irow, 4).Value) = Cells(irow, 6)
  8.         Else
  9.             Dic(Cells(irow, 4).Value) = Cells(irow, 6) + Dic(Cells(irow, 4).Value)
  10.         End If
  11.     Next
  12.     Rows("2:" & Dic.Count + 1).Insert 3  ‘因为是将结果全部插入到前面,先要知道需要多少行,利用dic.count即可获得所需行数,因为是从第二行开始插入,则所对应行的范围是第2行到dicount+2-1(即dic.count+1),先插入空白行
  13.     i = 2  ‘ 设定开始写入汇总的行数值
  14.         For irow = Dic.Count + 1 To [D65536].End(3).Row  ‘因插入空白行,现有数据下移了dic.count行,所以原数据区域的取值范围起始值变成了2+dic.count-1(即dic.count+1)到最末行
  15.         If Dic.exists(Cells(irow, 4).Value) And Cells(irow, 4) <> Cells(irow - 1, 4) Then  ‘如果当前行与上一行不一致,则说明到了交界处,判断字典中是否存在该对应值并写入到汇总数据当前行
  16.             Cells(i, 4) = Cells(irow, 4).Value & "小计"
  17.             Cells(i, 7) = Dic(Cells(irow, 4).Value)
  18.             Range(Cells(i, 4), Cells(i, 7)).Interior.Color = vbRed
  19.             i = i + 1  ‘汇总数据写入行要相应增加
  20.             Dic.Remove (Cells(irow, 4).Value)  ‘删除已写入值
  21.         End If
  22.     Next

  23.     Dic.RemoveAll
  24. End Sub
2楼
yzcyzc1023
thank you。
3楼
aob
看看高手的表演,学习了!
4楼
E林好汉
举一反三,赞一个!

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap