1234

ExcelHome技术论坛

用户名  找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

帖子
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
123
返回列表 发新帖
楼主: Laughingsun889

[求助] 多字段分类汇总问题

[复制链接]

TA的精华主题

TA的得分主题

发表于 2025-3-14 11:05 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 ykcbf1100 于 2025-3-14 11:07 编辑

按日期和工艺分类汇总,增加小计、累计、总计,非公式法

df330feb-8af0-4960-a20b-55fd77598301.png

分工艺自动小计合计.zip

22.59 KB, 下载次数: 9

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2025-3-14 11:06 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
  1. Sub ykcbf()   '//2025.3.14
  2.     Application.ScreenUpdating = False
  3.     Application.DisplayAlerts = False
  4.     bt = 1: col = 1: c = 2: c1 = 3
  5.     Dim zr(1 To 1000)
  6.     br = [{"小计","累计"}]
  7.     On Error Resume Next
  8.     With ActiveSheet
  9.         arr = .UsedRange
  10.         For i = bt + 1 To UBound(arr)
  11.             If InStr(arr(i, c), br(1)) Then
  12.                 MsgBox "已有小计行,不必再插入!"
  13.                 Exit Sub
  14.             End If
  15.         Next
  16.         .Cells.Interior.ColorIndex = 0
  17.         ReDim brr(1 To UBound(arr) * 2, 1 To 7)
  18.         ReDim Sum(1 To 7)
  19.         ReDim Ss(1 To 7)
  20.         For i = bt + 1 To UBound(arr)
  21.             If Month(arr(i, col)) & arr(i, 2) <> Month(arr(i - 1, col)) & arr(i - 1, 2) Then k = k + 1: zr(k) = Array(i, i)
  22.             zr(k)(1) = i
  23.         Next
  24.         For x = 1 To k
  25.             r1 = zr(x)(0): r2 = zr(x)(1)
  26.             n = r2 - r1 + 1
  27.             For i = r1 To r2
  28.                 m = m + 1
  29.                 For j = 1 To UBound(arr, 2)
  30.                     brr(m, j) = arr(i, j)
  31.                 Next
  32.             Next
  33.             m = m + 2
  34.             brr(m - 1, 2) = Month(arr(r1, 1)) & "月" & arr(r1, 2) & br(1)
  35.             brr(m, 2) = Month(arr(r1, 1)) & "月" & br(2)
  36.             For j = c1 To UBound(arr, 2)
  37.                 brr(m - 1, j) = Application.Sum(.Cells(r1, j).Resize(n))
  38.                 Ss(j) = Ss(j) + brr(m - 1, j)
  39.                 If x = 1 Or Month(arr(r2, col)) <> Month(arr(zr(x - 1)(1), col)) Then
  40.                     brr(m, j) = brr(m - 1, j)
  41.                     Sum(j) = brr(m - 1, j)
  42.                 Else
  43.                     Sum(j) = Sum(j) + brr(m - 1, j)
  44.                 End If
  45.                 brr(m, j) = Sum(j)
  46.             Next
  47.         Next
  48.         m = m + 1
  49.         brr(m, 2) = "总计"
  50.         For j = c1 To UBound(arr, 2)
  51.             brr(m, j) = Ss(j)
  52.         Next
  53.         With .Cells(bt + 1, 1).Resize(m, UBound(arr, 2))
  54.             .Value = brr
  55.             .Borders.LineStyle = 1
  56.             .HorizontalAlignment = xlCenter
  57.             .VerticalAlignment = xlCenter
  58.         End With
  59.         For i = bt + 1 To m + bt
  60.             Set Rng = .Cells(i, c)
  61.             If InStr(Rng.Value, "小计") Then
  62.                 Rng.Resize(, 6).Interior.ColorIndex = 4
  63.                 Rng.Offset(1).Resize(, 6).Interior.ColorIndex = 6
  64.             End If
  65.             If InStr(Rng.Value, "总计") Then
  66.                 Rng.Resize(, 6).Interior.ColorIndex = 8
  67.             End If
  68.         Next
  69.         ActiveWindow.DisplayZeros = False
  70.     End With
  71.     Application.ScreenUpdating = True
  72.     MsgBox "OK!"
  73. End Sub

  74. Sub 删除小计()
  75.     With ActiveSheet
  76.         bt = 2: col = 2
  77.         r = .Cells(.Rows.Count, col).End(xlUp).Row
  78.         Set Rng = Nothing
  79.         For i = bt + 1 To r
  80.             If InStr(.Cells(i, col), "计") Then
  81.                 If Rng Is Nothing Then
  82.                     Set Rng = .Rows(i)
  83.                 Else
  84.                     Set Rng = Union(Rng, .Rows(i))
  85.                 End If
  86.             End If
  87.         Next
  88.         If Not Rng Is Nothing Then Rng.EntireRow.Delete
  89.     End With
  90. End Sub
复制代码


TA的精华主题

TA的得分主题

发表于 2025-3-14 15:00 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

1234

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-4-23 23:42 , Processed in 0.018372 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表