ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: 彭希仁

[讨论]有点难度的汇总

[复制链接]

TA的精华主题

TA的得分主题

发表于 2007-11-29 20:21 | 显示全部楼层

彭兄,检查一下,morganwong兄的是否合你的题意,

如果是,给我们一个警钟,不要什么都一定要VBA解决.

TA的精华主题

TA的得分主题

发表于 2007-11-29 20:25 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
这个是纯粹用ado打开纪录集来做的,代码不多,但不知道速度如何。其中左边的计数是按机型和故障类型来汇总的。右边的三个计数是按机型、故障类型和地区故障数前三名来汇总的。那个占比是什么不清楚,所以没有做。 7thDZdKK.rar (24.68 KB, 下载次数: 11)

DYxNQjtf.rar

195.31 KB, 下载次数: 8

[讨论]有点难度的汇总

KrPjMoz5.rar

195.31 KB, 下载次数: 9

[讨论]有点难度的汇总

TA的精华主题

TA的得分主题

 楼主| 发表于 2007-11-29 23:00 | 显示全部楼层

Sub xi()
    Dim h As Long
    Dim i As Long
    Dim j As Long
    Dim x As Long
    Dim y As Long
    Dim z As Long
  Application.ScreenUpdating = False
    t = Timer
Sheet2.Select
    r = Sheet2.[A65536].End(xlUp).Row - 1
    Sheet2.Range("a2").Resize(r, 3).sort Key1:=Range("B2"), Order1:=xlAscending, Key2:=Range("C2") _
                                       , Order2:=xlAscending, Key3:=Range("A2"), Order3:=xlAscending

    arr = Sheet2.Cells(1, 1).Resize(r + 2, 5)
    For i = 2 To r                          '为加速做准备
        arr(i, 4) = arr(i, 2) & arr(i, 3)
        arr(i, 5) = arr(i, 1) & arr(i, 2) & arr(i, 3)
    Next i
    ReDim arr1(1 To r, 1 To 11)
    ReDim arr2(1 To r, 1 To 2)
    For i = 2 To r + 1
        If arr(i, 5) = arr(i - 1, 5) Then                '地区计数
            arr2(x, 2) = arr2(x, 2) + 1
        Else
            x = x + 1
            arr2(x, 1) = i
            arr2(x, 2) = 1
        End If
        jh = jh + 1
        If arr(i, 4) = arr(i - 1, 4) Then               '故障计数
            arr1(y, 4) = arr1(y, 4) + 1
        Else
            y = y + 1
            arr1(y, 2) = arr(i, 2)
            arr1(y, 3) = arr(i, 3)
            arr1(y, 4) = 1
        End If
        If arr(i, 2) <> arr(i + 1, 2) Then            '生成合计
            y = y + 1
            arr1(y, 2) = arr(i, 2)
            arr1(y, 3) = "合计"
            arr1(y, 4) = jh
            jh = 0
        End If
    Next i
    For h = 7 To 11 Step 2
        j = 1
        For i = 1 To y - 1
            If arr1(i, 3) = "合计" Then i = i + 1
            For j = j To x
                If arr(arr2(j, 1), 4) = arr1(i, 2) & arr1(i, 3) Then
                    If arr2(j, 2) > arr1(i, h) Then            '记录大小和位置
                        arr1(i, h) = arr2(j, 2)
                        arr1(i, h - 1) = j
                    End If
                Else
                    If arr1(i, h) > 0 Then
                        arr2(arr1(i, h - 1), 2) = 0
                        arr1(i, h - 1) = arr(arr2(arr1(i, h - 1), 1), 1)
                    End If
                    Exit For
                End If

            Next j
        Next i
    Next h

    arr1(1, 1) = 1
    For i = 2 To y
        If arr1(i, 2) = arr1(i - 1, 2) Then
            arr1(i, 1) = arr1(i - 1, 1) + 1
        Else
            arr1(i, 1) = 1
        End If
    Next i
    For i = y To 1 Step -1
        If arr1(i, 3) = "合计" Then x = arr1(i, 4)
        arr1(i, 5) = arr1(i, 4) / x
    Next i
    Sheet3.Select
    Cells(3, 1).Resize(y, 11) = arr1
'    Cells(3, 2).Resize(y, 11).Select
 '   Selection.sort Key1:=Range("B3"), Order1:=xlAscending, Key2:=Range("D3") _
                 , Order2:=xlAscending
   Application.ScreenUpdating = True
    [p2] = Timer - t
End Sub

排序加数组是最快的方法.其实数组部分基本没有花时间,主要排序的时间.

[此贴子已经被作者于2007-11-29 23:39:37编辑过]

TA的精华主题

TA的得分主题

 楼主| 发表于 2007-11-29 23:09 | 显示全部楼层
QUOTE:
以下是引用morganwong在2007-11-29 19:06:32的发言:

用不着VBA

我还是那句话,要转换思维,此路不通,则彼路通!!

用辅助列的方式!!



我最早也是用if加辅助列实现,速度也不可以

数据量大时countif太多会慢死.

TA的精华主题

TA的得分主题

发表于 2007-11-30 00:01 | 显示全部楼层
QUOTE:
以下是引用彭希仁在2007-11-29 23:00:55的发言:

Sub xi()
    Dim h As Long
    Dim i As Long
    Dim j As Long
    Dim x As Long
    Dim y As Long
    Dim z As Long
  Application.ScreenUpdating = False
    t = Timer
Sheet2.Select
    r = Sheet2.[A65536].End(xlUp).Row - 1
    Sheet2.Range("a2").Resize(r, 3).sort Key1:=Range("B2"), Order1:=xlAscending, Key2:=Range("C2") _
                                       , Order2:=xlAscending, Key3:=Range("A2"), Order3:=xlAscending

    arr = Sheet2.Cells(1, 1).Resize(r + 2, 5)
    For i = 2 To r                          '为加速做准备
        arr(i, 4) = arr(i, 2) & arr(i, 3)
        arr(i, 5) = arr(i, 1) & arr(i, 2) & arr(i, 3)
    Next i
    ReDim arr1(1 To r, 1 To 11)
    ReDim arr2(1 To r, 1 To 2)
    For i = 2 To r + 1
        If arr(i, 5) = arr(i - 1, 5) Then                '地区计数
            arr2(x, 2) = arr2(x, 2) + 1
        Else
            x = x + 1
            arr2(x, 1) = i
            arr2(x, 2) = 1
        End If
        jh = jh + 1
        If arr(i, 4) = arr(i - 1, 4) Then               '故障计数
            arr1(y, 4) = arr1(y, 4) + 1
        Else
            y = y + 1
            arr1(y, 2) = arr(i, 2)
            arr1(y, 3) = arr(i, 3)
            arr1(y, 4) = 1
        End If
        If arr(i, 2) <> arr(i + 1, 2) Then            '生成合计
            y = y + 1
            arr1(y, 2) = arr(i, 2)
            arr1(y, 3) = "合计"
            arr1(y, 4) = jh
            jh = 0
        End If
    Next i
    For h = 7 To 11 Step 2
        j = 1
        For i = 1 To y - 1
            If arr1(i, 3) = "合计" Then i = i + 1
            For j = j To x
                If arr(arr2(j, 1), 4) = arr1(i, 2) & arr1(i, 3) Then
                    If arr2(j, 2) > arr1(i, h) Then            '记录大小和位置
                        arr1(i, h) = arr2(j, 2)
                        arr1(i, h - 1) = j
                    End If
                Else
                    If arr1(i, h) > 0 Then
                        arr2(arr1(i, h - 1), 2) = 0
                        arr1(i, h - 1) = arr(arr2(arr1(i, h - 1), 1), 1)
                    End If
                    Exit For
                End If

            Next j
        Next i
    Next h

    arr1(1, 1) = 1
    For i = 2 To y
        If arr1(i, 2) = arr1(i - 1, 2) Then
            arr1(i, 1) = arr1(i - 1, 1) + 1
        Else
            arr1(i, 1) = 1
        End If
    Next i
    For i = y To 1 Step -1
        If arr1(i, 3) = "合计" Then x = arr1(i, 4)
        arr1(i, 5) = arr1(i, 4) / x
    Next i
    Sheet3.Select
    Cells(3, 1).Resize(y, 11) = arr1
'    Cells(3, 2).Resize(y, 11).Select
 '   Selection.sort Key1:=Range("B3"), Order1:=xlAscending, Key2:=Range("D3") _
                 , Order2:=xlAscending
   Application.ScreenUpdating = True
    [p2] = Timer - t
End Sub

排序加数组是最快的方法.其实数组部分基本没有花时间,主要排序的时间.


好贴!!!

难得彭兄思路如此清楚,佩服之至。

对ADO+SQL有点失望了,呵呵

TA的精华主题

TA的得分主题

发表于 2007-11-30 00:02 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
我的代码算法有问题,太慢,不献丑了。

TA的精华主题

TA的得分主题

 楼主| 发表于 2007-11-30 08:06 | 显示全部楼层
QUOTE:
以下是引用northwolves在2007-11-30 0:01:29的发言:

好贴!!!

难得彭兄思路如此清楚,佩服之至。

对ADO+SQL有点失望了,呵呵

昨天我用ADO+SQL测试,第一次花了15秒,第二次花了53秒,第三次死机了。

看来要用ADO+SQL也可以但要尽量少用(但只能用一句),后面的运算都交给数组来完成。这样就可以省去排序。

SQL = "SELECT 省份,机型,故障,Count(*) as 故障数量 FROM[数据$A1:C65536] GROUP BY 机型,故障,省份"

另外ldy888兄的字典套的方法也很好,效率也很高,应该还有提速的空间。



TA的精华主题

TA的得分主题

发表于 2007-11-30 08:17 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
QUOTE:
以下是引用彭希仁在2007-11-29 23:00:55的发言:

Sub xi()
    Dim h As Long
    Dim i As Long
    Dim j As Long
    Dim x As Long
    Dim y As Long
    Dim z As Long
  Application.ScreenUpdating = False
    t = Timer
Sheet2.Select
    r = Sheet2.[A65536].End(xlUp).Row - 1
    Sheet2.Range("a2").Resize(r, 3).sort Key1:=Range("B2"), Order1:=xlAscending, Key2:=Range("C2") _
                                       , Order2:=xlAscending, Key3:=Range("A2"), Order3:=xlAscending

    arr = Sheet2.Cells(1, 1).Resize(r + 2, 5)
    For i = 2 To r                          '为加速做准备
        arr(i, 4) = arr(i, 2) & arr(i, 3)
        arr(i, 5) = arr(i, 1) & arr(i, 2) & arr(i, 3)
    Next i
    ReDim arr1(1 To r, 1 To 11)
    ReDim arr2(1 To r, 1 To 2)
    For i = 2 To r + 1
        If arr(i, 5) = arr(i - 1, 5) Then                '地区计数
            arr2(x, 2) = arr2(x, 2) + 1
        Else
            x = x + 1
            arr2(x, 1) = i
            arr2(x, 2) = 1
        End If
        jh = jh + 1
        If arr(i, 4) = arr(i - 1, 4) Then               '故障计数
            arr1(y, 4) = arr1(y, 4) + 1
        Else
            y = y + 1
            arr1(y, 2) = arr(i, 2)
            arr1(y, 3) = arr(i, 3)
            arr1(y, 4) = 1
        End If
        If arr(i, 2) <> arr(i + 1, 2) Then            '生成合计
            y = y + 1
            arr1(y, 2) = arr(i, 2)
            arr1(y, 3) = "合计"
            arr1(y, 4) = jh
            jh = 0
        End If
    Next i
    For h = 7 To 11 Step 2
        j = 1
        For i = 1 To y - 1
            If arr1(i, 3) = "合计" Then i = i + 1
            For j = j To x
                If arr(arr2(j, 1), 4) = arr1(i, 2) & arr1(i, 3) Then
                    If arr2(j, 2) > arr1(i, h) Then            '记录大小和位置
                        arr1(i, h) = arr2(j, 2)
                        arr1(i, h - 1) = j
                    End If
                Else
                    If arr1(i, h) > 0 Then
                        arr2(arr1(i, h - 1), 2) = 0
                        arr1(i, h - 1) = arr(arr2(arr1(i, h - 1), 1), 1)
                    End If
                    Exit For
                End If

            Next j
        Next i
    Next h

    arr1(1, 1) = 1
    For i = 2 To y
        If arr1(i, 2) = arr1(i - 1, 2) Then
            arr1(i, 1) = arr1(i - 1, 1) + 1
        Else
            arr1(i, 1) = 1
        End If
    Next i
    For i = y To 1 Step -1
        If arr1(i, 3) = "合计" Then x = arr1(i, 4)
        arr1(i, 5) = arr1(i, 4) / x
    Next i
    Sheet3.Select
    Cells(3, 1).Resize(y, 11) = arr1
'    Cells(3, 2).Resize(y, 11).Select
 '   Selection.sort Key1:=Range("B3"), Order1:=xlAscending, Key2:=Range("D3") _
                 , Order2:=xlAscending
   Application.ScreenUpdating = True
    [p2] = Timer - t
End Sub

排序加数组是最快的方法.其实数组部分基本没有花时间,主要排序的时间.


要是这样的话,还是快不过字典套字典啊!那个字典套字典没发挥它最高的效率!

没有那个三重排序,后面的数组根本没用!

[此贴子已经被作者于2007-11-30 8:34:12编辑过]

TA的精华主题

TA的得分主题

 楼主| 发表于 2007-11-30 08:43 | 显示全部楼层
QUOTE:
以下是引用tycp在2007-11-30 8:17:36的发言:

要是这样的话,还是快不过字典套字典啊!那个字典套字典没发挥它最高的效率!

要是这样的话,再加上EXCEL的分类汇总,筛选岂不是更快!

wRP9DIzx.rar (205.55 KB, 下载次数: 27)

以上是实际的数据,测试速度对比。


汇总的类别较多时,排序加数组的方法快些。较少时字典好些,字典套字典的方法潜力很大值得深究。

另外EXCEL的分类汇总的前题也是要排序的,而且不能直接出结果,还是得后期处理,所以速度不占有优势。

[此贴子已经被作者于2007-11-30 8:46:34编辑过]

TA的精华主题

TA的得分主题

发表于 2007-11-30 09:14 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

笨办法,Consolidate加数组,速度有些慢,用62楼数据18秒。请高手指正。

Sub 汇总1()
   Application.ScreenUpdating = 0
    Dim arr, arr1, arr2, arr3, arr4
    t = Timer
    sj = "数据"
    bb = "报表"
    ls = "临时"
    Sheets(bb).Select
    Set NewSheet = Worksheets.Add
    NewSheet.Name = ls
    Sheets(sj).Select
    n = [a65536].End(xlUp).Row
    ReDim arr(1 To n, 1 To 5)
    For i = 1 To n
        arr(i, 1) = Trim(Cells(i, 1))
        arr(i, 2) = Trim(Cells(i, 2))
        arr(i, 3) = Trim(Cells(i, 3))
        If i = 1 Then
           arr(i, 4) = "数量"
           arr(i, 5) = arr(i, 2) & arr(i, 3)
        Else: arr(i, 4) = 1
           arr(i, 5) = arr(i, 2) & "-" & arr(i, 3)
        End If
    Next i
   
    Sheets(ls).Select
    Range("a1:a" & n) = Application.Index(arr, , 5)
    Range("b1:b" & n) = Application.Index(arr, , 4)
    s = ls & "!R1C1:R" & n & "C2"
    Sheets(ls).[c1].Consolidate s, xlSum, 1, 1
    x = [c65536].End(xlUp).Row
    arr1 = Range("c2:d" & x)
    Range("a:d").Delete
    ReDim jxgz(1 To UBound(arr1), 1 To 3)
    For i = 1 To UBound(arr1)
        jxgz(i, 1) = Split(arr1(i, 1), "-")(0)
        jxgz(i, 2) = Split(arr1(i, 1), "-")(1)
        jxgz(i, 3) = arr1(i, 2)
    Next i
    Range("a2:a" & UBound(jxgz) + 1) = Application.Index(jxgz, , 1)
    Range("a1") = "机型"
    x = [a1].End(xlDown).Row
    Range("a1:a" & x).AdvancedFilter 2, , [b1], 1
    y = [b1].End(xlDown).Row
    Range("c2:c" & y).FormulaR1C1 = "=COUNTIF(R2C1:R" & x & "C1,RC[-1])"
    arr2 = Range("b2:c" & y)
    gzzl = Application.Sum(Range("c2:c" & y))
    Range("a:d").Delete
    ReDim hz(1 To gzzl + y - 1, 1 To 11)
   
    For i1 = 1 To UBound(arr2)
        x = arr2(i1, 1)
        y = arr2(i1, 2)
        ReDim arr3(1 To y, 1 To 2)
        k = 0
        g = 0
        For i2 = 1 To UBound(jxgz)
            If jxgz(i2, 1) = x Then
               k = k + 1
               arr3(k, 1) = jxgz(i2, 2)
               arr3(k, 2) = jxgz(i2, 3)
               g = g + jxgz(i2, 3)
            End If
        Next i2
        For i3 = 1 To UBound(arr3) + 1
          l = l + 1
          hz(l, 1) = i3
          hz(l, 2) = x
          If i3 <= UBound(arr3) Then
            a = arr3(i3, 1)
            hz(l, 3) = a
            b = arr3(i3, 2)
            hz(l, 4) = b
            hz(l, 5) = Round(b / g, 4)
            ReDim arr4(1 To b, 1 To 2)
            k = 0
            For i4 = 2 To UBound(arr) Step 1
                If arr(i4, 5) = x & "-" & a Then
                   k = k + 1
                   arr4(k, 1) = arr(i4, 1)
                   arr4(k, 2) = arr(i4, 4)
                End If
             Next i4
             Range("a1") = arr(1, 1)
             Range("b1") = arr(1, 4)
             Range("a2:a" & b + 1) = Application.Index(arr4, , 1)
             Range("b2:b" & b + 1) = Application.Index(arr4, , 2)
             s = ls & "!R1C1:R" & b + 1 & "C2"
             Sheets(ls).[c1].Consolidate s, xlSum, 1, 1
             m = [c65536].End(xlUp).Row
             Range("c1:d1").AutoFilter
             Range("c1:d" & m).Sort Key1:=Range("d1"), Order1:=xlDescending, Header:= _
                   xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                   SortMethod:=xlPinYin, DataOption1:=xlSortNormal
             hz(l, 6) = Range("c2").Value
             hz(l, 7) = Range("d2").Value
             hz(l, 8) = Range("c3").Value
             hz(l, 9) = Range("d3").Value
             hz(l, 10) = Range("c4").Value
             hz(l, 11) = Range("d4").Value
             Range("a:d").Delete
          Else: hz(l, 3) = "合计"
                hz(l, 4) = g
                hz(l, 5) = 1
          End If
        Next i3
    Next i1
    Application.DisplayAlerts = False
    Sheets(ls).Delete
    Application.DisplayAlerts = True
    Sheets(bb).Select
    Range("a3:k" & UBound(hz) + 2) = hz
    For i = 3 To UBound(hz) + 2 Step 1
        If Cells(i, 3) = "合计" Then
        Range("a" & i & ":k" & i).Interior.ColorIndex = 43
        End If
    Next
   Application.ScreenUpdating = 1
   Range("l2") = Timer - t
End Sub

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

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-29 02:44 , Processed in 0.071940 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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