ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > Excel VBA > 如何依条件生成汇总排序选项卡?

如何依条件生成汇总排序选项卡?

作者:绿色风 分类: 时间:2022-08-17 浏览:113
楼主
liuguansky
Q:如何依条件生成汇总排序选项卡?
根据条件,选择俗称跟月份得到每个月的的所有汇总情况。不良数是降序排序,汇总出前7项,超出的项目归为其它,不管数据源中“其它”的不良数有多少都是排在最后。生成示例如下所示:

A:用如下事件代码可以实现:
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.     If Target.Count > 1 Then Exit Sub
  3.     If Target.Address(0, 0) = "B2" Or Target.Address(0, 0) = "E2" Then
  4.         Application.EnableEvents = False
  5.         Dim str1$, m%, rng As Range
  6.         str1 = [b2]: m = [e2]
  7.         Dim k&, arr1, arr2, lc&, br&, i&, j&, arrt()
  8.         Dim str2$, dic, checkN&, badN&, t&, temp(1 To 3), n%
  9.         Set dic = CreateObject("scripting.dictionary")
  10.         With Sheet1
  11.             lc = .Cells(3, 1).End(2).Column - 4
  12.             Set rng = .Range("a:a").Find(str1, Range("A1"), , xlWhole)
  13.             If rng Is Nothing Then MsgBox "未找到该俗称,请核实": Exit Sub
  14.             br = rng.Row
  15.             k = rng.MergeArea.Cells.Count + 1
  16.             arr1 = .Cells(3, "e").Resize(1, lc).Value
  17.             arr2 = .Cells(br, "c").Resize(k, lc + 2).Value
  18.         End With
  19.         For i = 1 To lc
  20.             If Month(arr1(1, i)) = m Then
  21.                 checkN = checkN + arr2(k, i + 2)
  22.                 For j = 1 To k - 1
  23.                     str2 = arr2(j, 1) & vbTab & arr2(j, 2)
  24.                     badN = badN + arr2(j, i + 2)
  25.                     If Len(str2) > 0 Then
  26.                         If dic.exists(str2) Then
  27.                             arrt(3, dic(str2)) = arrt(3, dic(str2)) + arr2(j, i + 2)
  28.                             Else: t = t + 1: ReDim Preserve arrt(1 To 3, 1 To t)
  29.                                 dic.Add str2, t: arrt(1, t) = arr2(j, 1): arrt(2, t) = arr2(j, 2): arrt(3, t) = arr2(j, i + 2)
  30.                         End If
  31.                     End If
  32.                 Next j
  33.             End If
  34.         Next i
  35.         If t > 0 Then
  36.             For i = 1 To t - 1
  37.                 For j = i + 1 To t
  38.                     If arrt(3, i) < arrt(3, j) And arrt(1, i) <> "其它" And arrt(1, j) <> "其它" Then
  39.                         For n = 1 To 3
  40.                             temp(n) = arrt(n, j)
  41.                             arrt(n, j) = arrt(n, i)
  42.                             arrt(n, i) = temp(n)
  43.                         Next n
  44.                     End If
  45.             Next j, i
  46.             If t > 8 Then
  47.                 For i = 9 To t
  48.                     arrt(1, 8) = "其他": arrt(2, 8) = ""
  49.                     arrt(3, 8) = arrt(3, 8) + arrt(3, i)
  50.                 Next
  51.             End If
  52.         End If
  53.         ReDim Preserve arrt(1 To 3, 1 To 8)
  54.         [b5] = checkN: [e5] = badN
  55.         Range("b8:d15") = Application.Transpose(arrt)
  56.         Set dic = Nothing
  57.         Application.EnableEvents = True
  58.     End If
  59. End Sub

具体示例文件如下:
2楼
herelazy
学习啦,谢谢花花分享!

免责声明

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

评论列表
sitemap