設(shè)為首頁收藏本站Access中國

Office中國論壇/Access中國論壇

 找回密碼
 注冊

QQ登錄

只需一步,快速開始

返回列表 發(fā)新帖
查看: 3138|回復(fù): 2
打印 上一主題 下一主題

[模塊/函數(shù)] 用代碼操作Excel,實現(xiàn)分類匯總、頁面設(shè)置

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
1#
發(fā)表于 2004-11-26 19:17:00 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
這篇文章是《將窗體內(nèi)容導(dǎo)出到Excel并對其進行格式化http://ctxi.cn/forum.php?mod=viewthread&tid=22539

的高級話題。

當你將窗體的數(shù)據(jù)導(dǎo)出到Excel后,可能會碰到需要對報表數(shù)據(jù)進行排序、分類匯總、頁面設(shè)置等操作,下面我將我曾經(jīng)寫過的代碼公開給大家,與大家分享,希望能對你有所幫助:

        '排序

        MyXL.Worksheets(1).UsedRange.Select

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

        '分類統(tǒng)計

        

        Dim sTotalCol()

        Dim aTotalCol(10) As Integer

        Dim n As Integer

        

        '分類統(tǒng)計的序號

        i = 1

        For i = 0 To lstTarget.ListCount - 1

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

                Exit For

            End If

        Next i

        

        '分類統(tǒng)計的列數(shù)及列號

        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          '數(shù)值型

                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

'頁面設(shè)置

Sub PageSetup(iLastCol As Integer)

    On Error Resume Next

    With MyXL.Application.ActiveSheet.PageSetup

        .LeftHeader = ""

        .CenterHeader = ""

        .RightHeader = ""

        .LeftFooter = "&10打印日期&""Times New Roman,常規(guī)"":&D"

        .CenterFooter = "&10第&""Times New Roman,常規(guī)""&&""宋體,常規(guī)""頁&""Times New Roman,常規(guī)"" &""宋體,常規(guī)""共&""Times New Roman,常規(guī)""&N&""宋體,常規(guī)""頁"

        .RightFooter = "&10艷陽天客戶管理系統(tǒng)&""Times New Roman,常規(guī)""V3.0 Tel:13714680826 www.szyyt.com"

        If iLastCol > 10
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享分享 分享淘帖 訂閱訂閱

點擊這里給我發(fā)消息

2#
發(fā)表于 2004-11-28 18:27:00 | 只看該作者
非常精彩,值得一讀。
3#
發(fā)表于 2004-11-28 20:48:00 | 只看該作者
It was wonderful.
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則

QQ|站長郵箱|小黑屋|手機版|Office中國/Access中國 ( 粵ICP備10043721號-1 )  

GMT+8, 2024-10-23 08:35 , Processed in 0.148947 second(s), 26 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回復(fù) 返回頂部 返回列表