Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2214|回复: 3

[已解决]查找多行中多条件的同一人,合计数据后,删除其它行怎么编写VBA

[复制链接]
发表于 2016-1-11 19:17 | 显示全部楼层 |阅读模式
         各位大侠、老师:我的表格有几千行,行内有不少是同一个人。    假设第1、5、8、21列均相同的,判断为同一个人,则将判断为同一个人的所有人的第22列数据相加,之后只保留求得之和的一行,其它行全部删除。祥见附件。这个VBA怎么编写啊,谢谢!!!

最佳答案
2016-1-11 20:17
Public Sub ss()
    Dim d
    Dim i, k
    Set d = CreateObject("scripting.dictionary")
    k = Cells(3, 1).End(xlDown).Row
    For i = 4 To k
        If i > k Then Exit For
        If d.exists(Cells(i, 1) & Cells(i, 5) & Cells(i, 8) & Cells(i, 21)) Then
            Cells(d(Cells(i, 1) & Cells(i, 5) & Cells(i, 8) & Cells(i, 21)), 22) = Cells(d(Cells(i, 1) & Cells(i, 5) & Cells(i, 8) & Cells(i, 21)), 22) + Cells(i, 22)
            Rows(i).Delete
            k = k - 1
            i = i - 1
        Else
            d(Cells(i, 1) & Cells(i, 5) & Cells(i, 8) & Cells(i, 21)) = i
        End If
    Next i
End Sub

Book1.zip

5.76 KB, 下载次数: 11

发表于 2016-1-11 19:51 | 显示全部楼层
如果对vba是零基础的话,建议把表格设置的跟你实际工作的一样.不然写好  你还是不会修改的
回复

使用道具 举报

发表于 2016-1-11 20:17 | 显示全部楼层    本楼为最佳答案   
Public Sub ss()
    Dim d
    Dim i, k
    Set d = CreateObject("scripting.dictionary")
    k = Cells(3, 1).End(xlDown).Row
    For i = 4 To k
        If i > k Then Exit For
        If d.exists(Cells(i, 1) & Cells(i, 5) & Cells(i, 8) & Cells(i, 21)) Then
            Cells(d(Cells(i, 1) & Cells(i, 5) & Cells(i, 8) & Cells(i, 21)), 22) = Cells(d(Cells(i, 1) & Cells(i, 5) & Cells(i, 8) & Cells(i, 21)), 22) + Cells(i, 22)
            Rows(i).Delete
            k = k - 1
            i = i - 1
        Else
            d(Cells(i, 1) & Cells(i, 5) & Cells(i, 8) & Cells(i, 21)) = i
        End If
    Next i
End Sub
回复

使用道具 举报

 楼主| 发表于 2016-1-12 11:03 | 显示全部楼层
本帖最后由 KDZ 于 2016-1-12 15:52 编辑
excel助手 发表于 2016-1-11 20:17
Public Sub ss()
    Dim d
    Dim i, k

老师:不匹配的小问题解决了,谢谢!!!!

Book2.zip

26.92 KB, 下载次数: 7

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-6-5 06:36 , Processed in 0.229615 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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