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. 《算法C语言实现》————三道题目

    1.对于N = 10,100和1000,记录你的运行环境中分别运行一下程序所花费的时间.(用python) import datetime global a a = 0 def time_1(s): ...

  2. jQuery ajax 请求HttpServlet返回[HTTP/1.1 405 Method not allowed]

    1.问题使用jQuery的ajax请求 Servlet 时,返回没有进入ajax的success回调函数,浏览器控制台显示 [HTTP/1.1 405 Method not allowed]. 2.解 ...

  3. python webdriver 测试框架-行为驱动例子

    安装行为驱动模块lettuce(卷心菜)模块 pip install lettuce Successfully installed argparse-1.4.0 colorama-0.3.9 extr ...

  4. $.ajax({ }) 里面的success函数不执行 | 回调函数返回的值 用对象,下标,键值对访问不到时

    原因一般是  dataType:'json' 数据类型设置成了json  ,去掉这个设置即可 $.ajax({ }) 回调函数返回的值 用对象,下标,键值对访问不到时,考虑是否返回数据为字符串 考虑是 ...

  5. java,swift,oc互相转换,html5 web开发跨平台

    java,swift,oc互相转换,html5 web开发跨平台 写一个java->swift的程序,这个程序是做跨平台系统的核心部分swift和oc到java也在考虑之列Swift->J ...

  6. python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑

    python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑 许多人在安装Python第三方库的时候, 经常会为一个问题困扰:到底应该下载什么格式的文件?当我们点开下载页时, 一般 ...

  7. SQL、SQL Server、MySQL与Oracle

    SQL (Structured Query Language),结构化查询语言,用来与多种数据库建立联系,根据ANSI(美国国家标准协会)的规定,SQL为RDBMS(关系型数据库)的标准语言. --- ...

  8. 《网络攻防》实验八:Web基础

    适逢多事之际,下周二的课设答辩.全国信安竞赛初赛作品筹备.协会密码沙龙比肩接踵,这些"案牍"不仅劳形还影响了我的复习计划."甘蔗没有两头甜的"还是要有所舍得了, ...

  9. Python3基础 os listdir curdir pardir 查看工作目录及其上一级目录的所有文件名

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  10. BZOJ3942: [Usaco2015 Feb]Censoring 栈+KMP

    Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so ...