20170711xlVBA自定义分类汇总一例

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

  

时间: 2024-07-30 13:51:58

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

20170928xlVBA自定义分类汇总

SubtotalByCQL Range("A1:E100").Value, "Select 1,2,Sum(4),Count(4) GroupBy 1,2", Range("J1"), True Sub SubtotalByCQL(ByVal Arr As Variant, ByVal CQL As String, ByVal DesRange As Range, Optional Header As Boolean = False) Dim i

模块管理常规功能自定义系统的设计与实现(41--终级阶段 综合查询[8]分类汇总)

综合查询(8)--分类汇总 这一节来看看分类汇总,在一个查询方案建立好了以后,可以对其进行汇总和分类汇总.分类汇总的级数可以任意级,为了方便操作,暂定为最多三级. 先来看看总计. 在上图显示总计前面就是一个分组设置,当前状态是未选择,我先选择一个按年度分组. 下面显示明细,看看结果. 这是一级分类汇总的,下面看看二级的. 再看看显示明细数据的 下面设置第三级: 三级显示明细 导出的excel表 理论上可以无限级次的分组,我暂定最多设置了三级. 博客是不是写得很失败啊,评论的人都没有一个.这一节也

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

今天在看博客的时候,无意中发现了@Trinea在GitHub上的一个项目Android开源项目分类汇总,由于类容太多了,我没有一个个完整地看完,但是里面介绍的开源项目都非常有参考价值,包括很炫的界面特效设计.个性化控件.工具库.优秀的Android开源项目.开发测试工具.优秀个人和团体等.可以这样说,每一位Andorid开发人员都能从中找到一个或多个适用自己项目的解决方案,消化吸收并加以利用,可以为自己的APP增色不少.文章最后还列出了部分国外著名Android开发者的信息,包括GitHub地址

【Anroid】Android开源项目分类汇总

Android开源项目第一篇——个性化控件(View)篇  包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView.ScrollView.TimeView.TipView.FlipView.ColorPickView.GraphView.UI Style.其他Android开源项目第二篇——工具库篇  包括依赖注入.图片缓存.网络相关.数据库ORM工具包.Android公共库.高版本向

Excel 学习笔记——排序,筛选,查找,定位,分类汇总和数据有效性及 细节操作技巧

Excel 学习笔记   课程内容:查找.替换.定位 想要实现的目标内容: 1.       替换指定内容,例:苏州 <- 苏州市  红色背景色<- 黄色背景色 将"张某某"替换为"经理的亲戚" 2.       定位特定位置的单元格,类似筛选功能(mac 系统中 暂时未发现定位按钮) 3.       批注 修改 删除 变换形状 隐藏命令 利用的工具,手段(操作按钮的名称,位置): 1.1   查找和替换-选项-单元格匹配 用来锁定指定单元格,避免类似

【Android】Android开源项目分类汇总

第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView.ScrollView.TimeView.TipView.FlipView.ColorPickView.GraphView.UI Style等等..其他 一.ListView android-pulltorefresh一个强大的拉动刷新开源项目,支持各种控件下拉刷新

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

Android开源项目分类汇总 如果你也对开源实现库的实现原理感兴趣,欢迎 Star 和 Fork Android优秀开源项目实现原理解析欢迎加入 QQ 交流群:383537512(入群理由需要填写群介绍问题答案) 377723625(一群已满) 欢迎大家推荐好的Android开源项目,可直接Commit,欢迎Star.Fork :) 目前包括: Android开源项目第一篇——个性化控件(View)篇  包括ListView.ActionBar.Menu.ViewPager.Gallery.G

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

From :http://blog.csdn.net/forlong401/article/details/25459403?c=6c4cd677a617db4655988e41ee081691#t7 Android开源项目分类汇总 欢迎大家推荐好的Android开源项目,可直接Commit或在 收集&提交页 中告诉我,欢迎Star.Fork :) 微博:Trinea    主页:www.trinea.cn    邮箱:[email protected]    QQ:717763774目前包括:

[Android_项目]Android开源项目分类汇总

第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView.ScrollView.TimeView.TipView.FlipView.ColorPickView.GraphView.UI Style等等..其他 一.ListView android-pulltorefresh一个强大的拉动刷新开源项目,支持各种控件下拉刷新