Public Sub CustomSubTotal()
AppSettings
On Error GoTo ErrHandler
Dim StartTime, UsedTime As Variant
StartTime = VBA.Timer
'Input code here Dim i As Long, j As Long, k
Dim Wb As Workbook
Dim Sht As Worksheet
Dim oSht As Worksheet
Dim Dic As Object
Dim Arr As Variant
Dim Rng As Range
Set Dic = CreateObject("Scripting.Dictionary")
Dim SendDate$, Client$, Cargo$, Style$, Num# Set Wb = Application.ThisWorkbook
Set Sht = Wb.Worksheets("数据表")
Set oSht = Wb.Worksheets("统计表")
With Sht
endrow = .Cells.Find("*", .Cells(1, 1), xlValues, xlWhole, xlByRows, xlPrevious).Row
Set Rng = .Range("A2:Z" & endrow)
Arr = Rng.Value
For i = LBound(Arr) To UBound(Arr)
SendDate = Format(CStr(Arr(i, 2)), "yyyy年mm月")
'Debug.Print mydate
Client = Arr(i, 4)
If Client = "" Then Client = "空"
Cargo = Arr(i, 5)
If Cargo = "" Then Cargo = "空"
Num = Arr(i, 10)
If InStr(1, Arr(i, 8), ",") > 0 Then
Style = Split(Arr(i, 8), ",")(0)
Else
Style = Arr(i, 8)
End If
'Debug.Print Style Key = SendDate & ";" & Client & ";" & Cargo & ";" & Style
Dic(Key) = Dic(Key) + Num Next i End With With oSht
.Cells.Clear
.Range("A1:E1").Value = Array("月份", "客户", "货品", "花色", "数量")
Arr = SubTotalDicToArr(Dic, ";")
.Range("A2").Resize(UBound(Arr), UBound(Arr, 2)).Value = Arr CustomSort .Range("A1").CurrentRegion
SetEdges .Range("A1").CurrentRegion End With UsedTime = VBA.Timer - StartTime
Debug.Print "UsedTime:" & Format(UsedTime, "0.000 Seconds") ErrorExit:
AppSettings False Set Wb = Nothing
Set Sht = Nothing
Set oSht = Nothing
Set Rng = Nothing
Set Dic = Nothing Exit Sub
ErrHandler:
If Err.Number <> 0 Then
MsgBox Err.Description & "!", vbCritical, "NextSeven QQ 84857038"
Debug.Print Err.Description
Err.Clear
Resume ErrorExit
End If
End Sub
Public Sub AppSettings(Optional IsStart As Boolean = True)
If IsStart Then
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Application.StatusBar = ">>>>>>>>Macro Is Running>>>>>>>>"
Else
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.StatusBar = False
End If
End Sub
Public Function SubTotalDicToArr(ByVal Dic As Object, ByVal Separator As String) As Variant()
Dim Arr(), OneKey, Key$, Item$, iRow&, iCol&
Dim Keys, Items, m&, n&, KeyCount&, ItemCount&
iCol = 0
For Each OneKey In Dic.Keys
iCol = UBound(Split(OneKey, Separator)) + 1
iCol = iCol + UBound(Split(Dic(OneKey), Separator)) + 1
Exit For
Next OneKey
iRow = Dic.Count
ReDim Arr(1 To iRow, 1 To iCol)
m = 0
For Each OneKey In Dic.Keys
m = m + 1
Keys = Split(OneKey, Separator)
KeyCount = UBound(Keys) + 1
For n = 1 To KeyCount
Arr(m, n) = Keys(n - 1)
Next n
Items = Split(Dic(OneKey), Separator)
ItemCount = UBound(Items) + 1
For n = 1 To ItemCount
Arr(m, KeyCount + n) = Items(n - 1)
Next n
Next OneKey
SubTotalDicToArr = Arr
End Function Private Sub SetEdges(ByVal Rng As Range)
With Rng
.HorizontalAlignment = xlCenter
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
If .Cells.Count > 1 Then
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
End With
End Sub
Sub CustomSort(ByVal RngWithTitle As Range)
With RngWithTitle
.Sort Key1:=RngWithTitle.Cells(1, 1), Order1:=xlAscending, _
Key2:=RngWithTitle.Cells(1, 2), Order2:=xlAscending, Header:=xlYes, _
MatchCase:=False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin
End With
End Sub

  

20170711xlVBA自定义分类汇总一例的更多相关文章

  1. 20170928xlVBA自定义分类汇总

    SubtotalByCQL Range("A1:E100").Value, "Select 1,2,Sum(4),Count(4) GroupBy 1,2", ...

  2. GitHub上史上最全的Android开源项目分类汇总 (转)

    GitHub上史上最全的Android开源项目分类汇总 标签: github android 开源 | 发表时间:2014-11-23 23:00 | 作者:u013149325 分享到: 出处:ht ...

  3. GitHub上史上最全的Android开源项目分类汇总

    今天在看博客的时候,无意中发现了 @Trinea 在GitHub上的一个项目 Android开源项目分类汇总 ,由于类容太多了,我没有一个个完整地看完,但是里面介绍的开源项目都非常有参考价值,包括很炫 ...

  4. Android 开源项目分类汇总(转)

    Android 开源项目分类汇总(转) ## 第一部分 个性化控件(View)主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Galler ...

  5. Android 开源项目分类汇总

    Android 开源项目分类汇总 Android 开源项目第一篇——个性化控件(View)篇  包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView ...

  6. Android开源项目分类汇总【畜生级别】[转]

    Android开源项目分类汇总 欢迎大家推荐好的Android开源项目,可直接Commit或在 收集&提交页 中告诉我,欢迎Star.Fork :) 微博:Trinea    主页:www.t ...

  7. Android开源项目分类汇总[转]

    Android开源项目分类汇总 如果你也对开源实现库的实现原理感兴趣,欢迎 Star 和 Fork Android优秀开源项目实现原理解析欢迎加入 QQ 交流群:383537512(入群理由需要填写群 ...

  8. Android开源项目分类汇总【畜生级别】

    From :http://blog.csdn.net/forlong401/article/details/25459403?c=6c4cd677a617db4655988e41ee081691#t7 ...

  9. Android开源项目分类汇总-转载

    太长了,还是转载吧...今天在看博客的时候,无意中发现了@Trinea在GitHub上的一个项目Android开源项目分类汇总,由于类容太多了,我没有一个个完整地看完,但是里面介绍的开源项目都非常有参 ...

随机推荐

  1. EXT3.0在IE下Range不兼容解决办法

    采用EXT3.0创建一个提示框,IE9下显示异常.经过资料查询,发现添加如下代码即可解决问题. // 这段代码是为了兼容IE if ((typeof Range !== "undefined ...

  2. linux基础命令---tmpwatch

    tmpwatch 删除最近一段时间没有访问的文件,时间以小时为单位,节省磁盘空间.tmpwatch递归删除给定时间未被访问的文件.通常,它用于清理用于临时保存空间(如/tmp)的目录.当更改目录时,t ...

  3. python之路----进程(一)

    一.理论知识1.操作系统发展简介 1.没有操作系统 —— 穿孔卡片 2.批处理系统 —— 串行 ,速度块 联机批处理 读磁带的时候速度快 脱机批处理 读磁带和cpu工作并发 3.多道程序系统 —— 并 ...

  4. Java系列笔记(5) - 线程

    我想关注这个系列博客的粉丝们都应该已经发现了,我一定是个懒虫,在这里向大家道歉了.这个系列的博客是在我工作之余写的,经常几天才写一小节,不过本着宁缺毋滥的精神,所有写的东西都是比较精炼的.这篇文章是本 ...

  5. JavaScript Image对象 / Tabel对象 / Select对象 / Form对象

    JavaScript Image / Tabel / Select / Form 对象 版权声明:未经授权,严禁转载! Image 对象 Image 对象,代表 <img> 元素. < ...

  6. 20145305 《网络对抗》MSF基础应用

    实践过程及结果截图 主动攻击ms08_067 Kali的IP:10.43.46.93 靶机的IP:10.43.49.28 MS08_067远程漏洞攻击实践:Shell 攻击成功的结果 在kali上执行 ...

  7. 20145324王嘉澜《网络对抗技术》Web基础

    实践要求 ①Web前端HTML: 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML ②Web前端javascipt: 理解JavaScript ...

  8. linux内核分析 第18章读书笔记

    十八章 调试 一.内核调试概述 1.需要面对的 一个确定的bug 一个藏匿bug的内核版本 相关的内核代码的知识和运气 2.艰难的调试工作 重现bug很困难:大部分bug通常都不是行为可靠而且定义明确 ...

  9. [Linux] - Linux安装JDK

    https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html  <官方JDK下载 之后 ...

  10. centos6下通用二进制格式安装MySQL过程

    1.首先确保主机的MySQL没有运行 #ss -tnl  //查看有没有80端口 或者 #service mysqld stop 2.添加mysql用户和组 #id mysql  //首先查看mysq ...