设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2897|回复: 2
打印 上一主题 下一主题

[模块/函数] 用代码操作Excel,实现分类汇总、页面设置

[复制链接]
跳转到指定楼层
1#
发表于 2004-11-26 19:17:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这篇文章是《将窗体内容导出到Excel并对其进行格式化http://www.office-cn.net/forum.php?mod=viewthread&tid=22539

的高级话题。

当你将窗体的数据导出到Excel后,可能会碰到需要对报表数据进行排序、分类汇总、页面设置等操作,下面我将我曾经写过的代码公开给大家,与大家分享,希望能对你有所帮助:

        '排序

        MyXL.Worksheets(1).UsedRange.Select

        SortByCol "F6"        '按F6所在的列排序

        '分类统计

        

        Dim sTotalCol()

        Dim aTotalCol(10) As Integer

        Dim n As Integer

        

        '分类统计的序号

        i = 1

        For i = 0 To lstTarget.ListCount - 1

            If lstTarget.ItemData(i) = Me.cboCat Then

                Exit For

            End If

        Next i

        

        '分类统计的列数及列号

        n = 1

        For n = 1 To 10

            aTotalCol(n) = 0

        Next n

        n = 1

        For j = 0 To iCols - 1

            Select Case rstTmp.Fields(j).Type

            Case 4          '数值型

                aTotalCol(n) = j + 1

                n = n + 1

            Case 5          '货币型

                aTotalCol(n) = j + 1

                n = n + 1

            End Select

        Next j

        

        For n = 0 To 10

            If aTotalCol(n + 1) = 0 Then

                Exit For

            End If

        Next n

        

        Select Case n

        Case 1

            sTotalCol = Array(aTotalCol(1))

        Case 2

            sTotalCol = Array(aTotalCol(1), aTotalCol(2))

        Case 3

            sTotalCol = Array(aTotalCol(1), aTotalCol(2), aTotalCol(3))

        Case 4

            sTotalCol = Array(aTotalCol(1), aTotalCol(2), aTotalCol(3), aTotalCol(4))

        Case 5

            sTotalCol = Array(aTotalCol(1), aTotalCol(2), aTotalCol(3), aTotalCol(4), aTotalCol(5))

        Case 6

            sTotalCol = Array(aTotalCol(1), aTotalCol(2), aTotalCol(3), aTotalCol(4), aTotalCol(5), aTotalCol(6))

        Case 7

            sTotalCol = Array(aTotalCol(1), aTotalCol(2), aTotalCol(3), aTotalCol(4), aTotalCol(5), aTotalCol(6), aTotalCol(7))

        Case 8

            sTotalCol = Array(aTotalCol(1), aTotalCol(2), aTotalCol(3), aTotalCol(4), aTotalCol(5), aTotalCol(6), aTotalCol(7), aTotalCol(8))

        Case 9

            sTotalCol = Array(aTotalCol(1), aTotalCol(2), aTotalCol(3), aTotalCol(4), aTotalCol(5), aTotalCol(6), aTotalCol(7), aTotalCol(8), aTotalCol(9))

        Case 10

            sTotalCol = Array(aTotalCol(1), aTotalCol(2), aTotalCol(3), aTotalCol(4), aTotalCol(5), aTotalCol(6), aTotalCol(7), aTotalCol(8), aTotalCol(9), aTotalCol(10))

        Case Else

            sTotalCol = Array(aTotalCol(1))

        End Select

        TotalByCat Trim(Str(i + 1)), sTotalCol

'按指定列排序

Sub SortByCol(sCol As String)

    MyXL.Application.Selection.Sort Key1:=Range(sCol), Order1:=xlAscending, Header:=xlGuess, _

        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _

        :=xlPinYin

End Sub

'分类汇总

Sub TotalByCat(iGroupBy As Integer, aTotalList As Variant)

    MyXL.Application.Selection.Subtotal GroupBy:=iGroupBy, Function:=xlSum, TotalList:=aTotalList _

        , Replace:=True, PageBreaks:=False, SummaryBelowData:=True

End Sub

'页面设置

Sub PageSetup(iLastCol As Integer)

    On Error Resume Next

    With MyXL.Application.ActiveSheet.PageSetup

        .LeftHeader = ""

        .CenterHeader = ""

        .RightHeader = ""

        .LeftFooter = "&10打印日期&""Times New Roman,常规"":&D"

        .CenterFooter = "&10第&""Times New Roman,常规""&&""宋体,常规""页&""Times New Roman,常规"" &""宋体,常规""共&""Times New Roman,常规""&N&""宋体,常规""页"

        .RightFooter = "&10艳阳天客户管理系统&""Times New Roman,常规""V3.0 Tel:13714680826 www.szyyt.com"

        If iLastCol > 10
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅

点击这里给我发消息

2#
发表于 2004-11-28 18:27:00 | 只看该作者
非常精彩,值得一读。
3#
发表于 2004-11-28 20:48:00 | 只看该作者
It was wonderful.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-5 02:22 , Processed in 0.095174 second(s), 26 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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