ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 454|回复: 13

[求助] 怎么根据条件求和

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-12-18 17:36 | 显示全部楼层 |阅读模式
先将表格的B、C、D、E、F列按升序排序,如果B、C、D、E列有相同的行,对应的F列合并、G列合并,计算H列合计面积。
Sub MergeAndSumData()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
   
    ' 遍历工作表
    For Each ws In ThisWorkbook.Worksheets
        ' 检查是否为需要操作的表格
        If ws.Name Like "b8" Then ' 将"Table*"替换为你要操作的表格的名称
            ' 获取最后一行数据
            lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
            
            ' 按B、C、D、E、F列按升序排序
            ws.Sort.SortFields.Clear
            ws.Sort.SortFields.Add Key:=Range("B3:B" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            ws.Sort.SortFields.Add Key:=Range("C3:C" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            ws.Sort.SortFields.Add Key:=Range("D3:D" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            ws.Sort.SortFields.Add Key:=Range("E3:E" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            ws.Sort.SortFields.Add Key:=Range("F3:F" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            With ws.Sort
                .SetRange Range("A3:I" & lastRow)
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            
            ' 合并F列和G列相同的值
            For i = lastRow To 2 Step -1
                If ws.Cells(i, "B").Value = ws.Cells(i - 1, "B").Value And _
                   ws.Cells(i, "C").Value = ws.Cells(i - 1, "C").Value And _
                   ws.Cells(i, "D").Value = ws.Cells(i - 1, "D").Value And _
                   ws.Cells(i, "E").Value = ws.Cells(i - 1, "E").Value Then
                    ws.Cells(i, "F").Value = ws.Cells(i, "F").Value & ", " & ws.Cells(i - 1, "F").Value
                    ws.Cells(i, "G").Value = ws.Cells(i, "G").Value & ", " & ws.Cells(i - 1, "G").Value
                    ws.Rows(i - 1).Delete
                End If
            Next i
            
            ' 求合同一组B、C、D、E列的H列
            lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
            For i = lastRow To 2 Step -1
                If ws.Cells(i, "B").Value = ws.Cells(i - 1, "B").Value And _
                   ws.Cells(i, "C").Value = ws.Cells(i - 1, "C").Value And _
                   ws.Cells(i, "D").Value = ws.Cells(i - 1, "D").Value And _
                   ws.Cells(i, "E").Value = ws.Cells(i - 1, "E").Value Then
                    ws.Cells(i, "H").Value = ws.Cells(i, "H").Value + ws.Cells(i - 1, "H").Value
                    ws.Rows(i - 1).Delete
                End If
            Next i
            
        End If
    Next ws
End Sub

以上代码能实现按B、C、D、E合并F和G列值,但H列求和不正确!!!!

工作簿1.rar

28.06 KB, 下载次数: 9

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-12-18 17:59 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
自己顶一下

TA的精华主题

TA的得分主题

发表于 2023-12-18 20:29 | 显示全部楼层
本帖最后由 shiruiqiang 于 2023-12-18 20:50 编辑

干嘛不字典多重嵌套?
image.jpg

TA的精华主题

TA的得分主题

发表于 2023-12-18 20:52 | 显示全部楼层
本帖最后由 shiruiqiang 于 2023-12-18 21:41 编辑

你想分页汇总还是把所有的表格汇总?
代码分页汇总(原代码有点错误,已修正)

工作簿1.rar

131.11 KB, 下载次数: 14

TA的精华主题

TA的得分主题

发表于 2023-12-18 20:52 | 显示全部楼层
本帖最后由 ykcbf1100 于 2023-12-18 21:08 编辑

更新一下,加上排序功能

工作簿1.7z

169.83 KB, 下载次数: 28

TA的精华主题

TA的得分主题

发表于 2023-12-18 20:53 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
代码如下:

  1. Sub ykcbf()  '//2023.12.18
  2.     Dim arr, brr, d, s
  3.     Set d = CreateObject("Scripting.Dictionary")
  4.     With Sheets("b9")
  5.         r = .Cells(Rows.Count, 1).End(3).Row
  6.         arr = .Range("a1:i" & r)
  7.         For i = 4 To UBound(arr)
  8.             s = arr(i, 2) & "|" & arr(i, 3) & "|" & arr(i, 4) & "|" & arr(i, 5)
  9.             If Not d.exists(s) Then
  10.                 d(s) = Array(arr(i, 2), arr(i, 3), arr(i, 4), arr(i, 5), arr(i, 6), arr(i, 7), arr(i, 8))
  11.             Else
  12.                 t = d(s)
  13.                 t(4) = t(4) & "、" & arr(i, 6)
  14.                 t(5) = t(5) & "、" & arr(i, 7)
  15.                 t(6) = t(6) + arr(i, 8)
  16.                 d(s) = t
  17.             End If
  18.         Next
  19.         t = d.items
  20.         .[k3:q1000] = ""
  21.         With .[k3].Resize(d.Count, 7)
  22.             .Value = Application.Rept(d.items, 1)
  23.             .Borders.LineStyle = 1
  24.             .HorizontalAlignment = xlCenter
  25.             .VerticalAlignment = xlCenter
  26.         End With
  27.     End With
  28.     Set d = Nothing
  29.     MsgBox "OK!"
  30. End Sub
复制代码


TA的精华主题

TA的得分主题

发表于 2023-12-18 21:17 | 显示全部楼层
工作簿1.rar (318.26 KB, 下载次数: 21)

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-12-21 08:33 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-12-21 16:32 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
shiruiqiang 发表于 2023-12-18 20:52
你想分页汇总还是把所有的表格汇总?
代码分页汇总(原代码有点错误,已修正)

大佬,我只是需要针对b8表进行统计汇总,其他的表不变,你的这个代码把每个表都统计汇总了,怎么把你的这个代码修改成只针对一个表

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-12-21 17:53 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-6-16 14:27 , Processed in 0.049941 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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