Excel精英培训网

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

用VBA进行分类汇总,特别是对数千行数据

[复制链接]
发表于 2012-2-20 19:18 | 显示全部楼层 |阅读模式
Sub MYDH()
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Range("A1:F3001").Select
    Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Key2:=Range("C2") _
        , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:= _
        xlSortNormal, DataOption2:=xlSortNormal
    Range("A1:F3001").Select
    Range("A6").Activate
    Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(5), _
        Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    Selection.Subtotal GroupBy:=3, Function:=xlSum, TotalList:=Array(5), _
        Replace:=False, PageBreaks:=False, SummaryBelowData:=True
    Range("A2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Range("A2:B3001").Select
    Range("A3").Activate
    Selection.Copy
    Range("A3").Select
    ActiveSheet.PasteSpecial Format:=1, Link:=1, DisplayAsIcon:=True, _
        IconFileName:=False
    Range("D2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Range("D2:D3001").Select
    Selection.Copy
    Range("D3").Select
    ActiveSheet.PasteSpecial Format:=1, Link:=1, DisplayAsIcon:=True, _
        IconFileName:=False
    Range("F2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Range("F2:F3001").Select
    Selection.FillDown
    Range("D2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Range("D2:D3001").Select
    Selection.Copy
    Range("D3").Select
    ActiveSheet.PasteSpecial Format:=1, Link:=1, DisplayAsIcon:=True, _
        IconFileName:=False
    Range("E:E").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.End(xlUp).Select
    Selection.End(xlToLeft).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Application.CutCopyMode = False
    Selection.AutoFilter
    Range("A2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Range(Selection, Selection.End(xlToLeft)).Select
    Range("A2:A3001").Select
    Selection.Copy
    Range("A3").Select
    ActiveSheet.PasteSpecial Format:=1, Link:=1, DisplayAsIcon:=True, _
        IconFileName:=False
    Range("C1").Select
    Selection.AutoFilter Field:=3, Criteria1:="=*汇总*", Operator:=xlAnd
    Range("B5:C15").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range("B5:C3100").Select
    Selection.NumberFormatLocal = "@"
    Range("B5").Select
End Sub
发表于 2012-2-20 19:21 | 显示全部楼层
这个宏够长的,也不容易理解,能简化一下吗?
回复

使用道具 举报

 楼主| 发表于 2012-2-20 19:28 | 显示全部楼层
雄鹰 发表于 2012-2-20 19:21
这个宏够长的,也不容易理解,能简化一下吗?

不能,因为这里面用到了两次排序,两次各四列的选择性粘贴及两次分类汇总的代码,是一种月报表汇总的好方法
回复

使用道具 举报

发表于 2012-2-20 20:36 | 显示全部楼层
bbhiox 发表于 2012-2-20 19:28
不能,因为这里面用到了两次排序,两次各四列的选择性粘贴及两次分类汇总的代码,是一种月报表汇总的好方法

看样子是录制的吧,手工的不会有这么多冗长的代码
回复

使用道具 举报

发表于 2012-2-20 21:56 | 显示全部楼层
看到好长的遗传数据啊,表示呕吐
回复

使用道具 举报

发表于 2012-2-20 22:55 | 显示全部楼层
bbhiox 发表于 2012-2-20 19:28
不能,因为这里面用到了两次排序,两次各四列的选择性粘贴及两次分类汇总的代码,是一种月报表汇总的好方法

录的代码虽然好用,但是可简化的东西太多...

简化后的速度和效率都比你录的高....
回复

使用道具 举报

发表于 2012-2-21 01:28 | 显示全部楼层
bbhiox 发表于 2012-2-20 19:28
不能,因为这里面用到了两次排序,两次各四列的选择性粘贴及两次分类汇总的代码,是一种月报表汇总的好方法

何不把你的文件发上来,看你是需要怎么样的汇总,可能用SQL方便哦
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-6 19:04 , Processed in 0.348491 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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