20170617xlVBA销售数据分类汇总

Public Sub SubtotalData()
    AppSettings
    ‘On Error GoTo ErrHandler
    Dim StartTime, UsedTime As Variant
    StartTime = VBA.Timer
    ‘Input code here

    Dim Wb As Workbook
    Dim Sht As Worksheet
    Dim oSht As Worksheet
    Dim Rng As Range
    Dim Arr As Variant

    Const HEAD_ROW As Long = 5
    Const SHEET_NAME As String = "分类汇总"
    Const START_COLUMN As String = "A"
    Const END_COLUMN As String = "Z"

    Const OTHER_HEAD_ROW As Long = 1
    ‘Const OTHER_SHEET_NAME As String = "DATA"
    Dim DataName As String
    Const OTHER_START_COLUMN As String = "A"
    Const OTHER_END_COLUMN As String = "Z"

    Dim Client As String    ‘客户名称
    Dim BookNo As String    ‘订单号
    Dim Status As String  ‘状态
    Dim Item As String    ‘统计项目
    Dim dClient As Object
    Dim dBookInfo As Object
    Dim MixKey As String
    Dim Key As String
    Dim TmpKey As String
    Dim OneClient
    Dim Index As Long

    Set dBookNo = CreateObject("Scripting.Dictionary")
    Set dBookInfo = CreateObject("Scripting.Dictionary")
    Set dClient = CreateObject("Scripting.Dictionary")

    Set Wb = Application.ThisWorkbook
    Set Sht = Wb.Worksheets(SHEET_NAME)
    With Sht
        .UsedRange.Offset(HEAD_ROW).ClearContents
        DataName = .Range("L2").Value
    End With

    If DataName = "" Then
        MsgBox "请输入查询范围!", vbInformation, "QQ "
        GoTo ErrorExit
    End If

    If DataName <> "全年" Then
        ‘判断某个月的!
        On Error Resume Next
        Set oSht = Wb.Worksheets(DataName)
        If oSht Is Nothing Then
            MsgBox "输入的月份(工作表名)有误,请重新输入!", vbInformation, "QQ "
            GoTo ErrorExit
        End If

        With oSht

            EndRow = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row
            Set Rng = .Range(.Cells(OTHER_HEAD_ROW + 1, "A"), .Cells(EndRow, "Y"))
            ‘Debug.Print Rng.Address
            Arr = Rng.Value

            For i = LBound(Arr) To UBound(Arr)
                Client = CStr(Arr(i, 2))    ‘客户名称

                BookNo = CStr(Arr(i, 1))
                Status = CStr(Arr(i, 6))    ‘进度状态

                dClient(Client) = ""    ‘保存所有客户名称

                MixKey = Client & ";" & BookNo & ";" & Status
                Key = Client & ";" & Status    ‘客户,状态

                If dBookNo.Exists(MixKey) = False Then    ‘防止重复
                    TmpKey = Key & ";" & "定单量"
                    ‘ dBookCount(TmpKey) = dBookCount(TmpKey) + 1
                    dBookInfo(TmpKey) = dBookInfo(TmpKey) + 1
                    dBookNo(MixKey) = ""    ‘记下订单号,防止重复
                End If

                TmpKey = Key & ";" & "订单金额"
                dBookInfo(TmpKey) = dBookInfo(TmpKey) + Arr(i, 12)

                TmpKey = Key & ";" & "已收款金额"
                dBookInfo(TmpKey) = dBookInfo(TmpKey) + Arr(i, 13)

                TmpKey = Key & ";" & "出库金额"
                dBookInfo(TmpKey) = dBookInfo(TmpKey) + Arr(i, 14)

                TmpKey = Key & ";" & "未收款金额"
                dBookInfo(TmpKey) = dBookInfo(TmpKey) + Arr(i, 15)

            Next i
        End With

    Else

        For Each oSht In Wb.Worksheets
            If oSht.Name Like "*月" Then
                With oSht

                    EndRow = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row
                    Set Rng = .Range(.Cells(OTHER_HEAD_ROW + 1, "A"), .Cells(EndRow, "Y"))
                    ‘Debug.Print Rng.Address
                    Arr = Rng.Value

                    For i = LBound(Arr) To UBound(Arr)
                        Client = CStr(Arr(i, 2))    ‘客户名称

                        BookNo = CStr(Arr(i, 1))
                        Status = CStr(Arr(i, 6))    ‘进度状态

                        dClient(Client) = ""    ‘保存所有客户名称

                        MixKey = Client & ";" & BookNo & ";" & Status
                        Key = Client & ";" & Status    ‘客户,状态

                        If dBookNo.Exists(MixKey) = False Then    ‘防止重复
                            TmpKey = Key & ";" & "定单量"
                            ‘ dBookCount(TmpKey) = dBookCount(TmpKey) + 1
                            dBookInfo(TmpKey) = dBookInfo(TmpKey) + 1
                            dBookNo(MixKey) = ""    ‘记下订单号,防止重复
                        End If

                        TmpKey = Key & ";" & "订单金额"
                        dBookInfo(TmpKey) = dBookInfo(TmpKey) + Arr(i, 12)

                        TmpKey = Key & ";" & "已收款金额"
                        dBookInfo(TmpKey) = dBookInfo(TmpKey) + Arr(i, 13)

                        TmpKey = Key & ";" & "出库金额"
                        dBookInfo(TmpKey) = dBookInfo(TmpKey) + Arr(i, 14)

                        TmpKey = Key & ";" & "未收款金额"
                        dBookInfo(TmpKey) = dBookInfo(TmpKey) + Arr(i, 15)

                    Next i
                End With

            End If
        Next oSht
    End If

    With Sht
        Index = 0
        For Each OneClient In dClient.keys
            Index = Index + 1
            .Cells(HEAD_ROW + Index, 1).Value = Index
            .Cells(HEAD_ROW + Index, 2).Value = OneClient

            For j = 3 To 12
                Status = .Cells(HEAD_ROW - 1, j).MergeArea.Cells(1, 1).Value
                Item = .Cells(HEAD_ROW, j).Value
                TmpKey = OneClient & ";" & Status & ";" & Item
                ‘ Debug.Print TmpKey
                .Cells(HEAD_ROW + Index, j).Value = dBookInfo(TmpKey)
                ‘Debug.Print Status
            Next j
        Next OneClient

        SetEdges Application.Intersect(.UsedRange.Offset(HEAD_ROW), .UsedRange)
    End With

    UsedTime = VBA.Timer - StartTime
    Debug.Print "UsedTime:" & Format(UsedTime, "0.000 Seconds")
    ‘MsgBox "UsedTime:" & Format(UsedTime, "0.000 Seconds"), vbOKOnly, "NextSeven  QQ "
ErrorExit:
    AppSettings False
    Exit Sub
ErrHandler:
    If Err.Number <> 0 Then
        MsgBox Err.Description & "!", vbCritical, "NextSeven "
        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

Private Sub SetEdges(ByVal Rng As Range)
    With Rng
        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

  

时间: 2024-08-12 04:31:58

20170617xlVBA销售数据分类汇总的相关文章

Excel数据分类汇总与数据透视表

苏轼的<题西林壁>:横看成岭侧成峰,远近高低各不同.给我们讲述着一个道理:同样的事物与内容,从不同角度观察会得到意想不到的结果.同样,Excel不单单只是一个数据的记录工具,也不单单是一个表格的制作工具,学会怎么从一行一行单调的数据去挖掘出我们想要的信息也是它的一个强项.我们不应小看Excel的挖掘功能,使用好挖掘功能会让我们得到意想不到的效果,将是我们工作中分析问题的一大助力. 下面介绍两种常用的数据分析.挖掘工具:数据分类汇总与数据透视表.要使用的示例数据如下: 呵呵-有点偷懒,还是使用上

SQLServer---使用Case When解决SQLServer数据分类汇总问题

SQLServer---使用Case When解决SQLServer数据分类汇总问题 近半年一直在负责某市的人事档案管理系统的后期开发和维护工作,之前客户给了一张如下图的表格,需要我去汇总数据,然后填充到表格中. 具体的需求:统计出每一个工作人员在某一段时间内分别打印了多少张不同的信函(或报表). 最初的想法 1.  查出使用该系统的工作人员 select realName as '姓名' from T_User where userID in(select distinct userID fr

[.NET] EF LINQ 按时间对数据分类汇总

======================================================== 作者:qiujuer 博客:blog.csdn.net/qiujuer网站:www.qiujuer.net开源库:Genius-Android转载请注明出处:http://blog.csdn.net/qiujuer/article/details/41868331======================================================== 发现国内

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

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

order_by_、group_by_、having的用法区别

写于 2012-11-20 22:14  doc文档上. Having 这个是用在聚合函数的用法.当我们在用聚合函数的时候,一般都要用到GROUP BY 先进行分组,然后再进行聚合函数的运算.运算完后就要用到HAVING 的用法了,就是进行判断了,例如说判断聚合函数的值是否大于某一个值等等. select customer_name,sum(balance) from balance group by customer_name having balance>200; yc_rpt_getnew

oracle

order by 从英文里理解就是行的排序方式,默认的为升序(asc). order by 后面必须列出排序的字段名,可以是多个字段名. group by 从英文里理解就是分组. 像sum().count().avg()等都是“聚合函数” 使用group by 的目的就是要将数据分类汇总. 一般如: select 单位名称,count(职工id),sum(职工工资) form [某表] group by 单位名称 这样的运行结果就是以“单位名称”为分类标志统计各单位的职工人数和工资总额. sel

Hadoop自定义分区Partitioner

一:背景 为了使得MapReduce计算后的结果显示更加人性化,Hadoop提供了分区的功能,可以使得MapReduce计算结果输出到不同的分区中,方便查看.Hadoop提供的Partitioner组件可以让Map对Key进行分区,从而可以根据不同key来分发到不同的reduce中去处理,我们可以自定义key的分发规则,如数据文件包含不同的省份,而输出的要求是每个省份对应一个文件. 二:技术实现 自定义分区很简单,我们只需要继承抽象类Partitioner,实现自定义的getPartitione

SQL 笔记 By 华仔

-------------------------------------读书笔记------------------------------- 笔记1-徐 最常用的几种备份方法 笔记2-徐 收缩数据库的大小的方法 笔记3-徐 设置数据库自动增长注意要点 笔记4-徐 模仿灾难发生时还原adventurework数据库 示例 stopat 笔记5-徐 检查日志文件不能被截断的原因 笔记6-徐 检测孤立用户并恢复孤立用户到新的服务器 解决数据库镜像孤立用户问题 笔记7-徐 SQLSERVER日志记录

《数据挖掘导论》 - 读书笔记(4) - 探索数据 [2016-8-20]

第3张 探索数据 第2章讨论知识发现过程中重要的高层数据问题.本章是数据探索,对数据进行初步研究,以便更好地理解它的特殊性质.数据探索有助于选择合适的数据预处理和数据分析技术.甚至可以处理一些通常由数据挖掘解决的问题.例如,有时可以通过对数据进行直观检查来发现模式. 本章包括三个主题:汇总统计.可视化和联机分析处理OLAP.汇总统计(如值集合的均值和标准差)和可视化技术是广泛用于数据探索的标准方法.OLAP的分析功能集中在从多为数据数组中创建汇总表的各种方法.OLAP技术包括在不同的维上或不同的