Filed under: Excel VBA 範例, Top, 一般程序

Excel檔案減肥瘦身

by chijanzen on 一月 18th, 2009 | View: 6,762 views

Tags
  • No Tags
Share Comments (9)
索   引 H0072
主   題 清除檔案中所有工作表上多餘的格式
版   本 >= 12.0(Office 2007)
說   明 檔案用久了有時候出現「記憶體不足」錯誤訊息或是覺得檔案越來越大,這時候就可以用這個程式來減肥啦!
參   考 用法:開啟H0072.xls檔,在工作表中按下減肥的按鈕,選取你要減肥的檔案就可以了


複製以下程式碼到Module

Code

Sub ClearExcessRowsAndColumns()
Dim ar As Range, r As Double, c As Double, tr As Double, tc As Double
Dim wksWks As Worksheet, ur As Range, arCount As Integer, i As Integer
Dim blProtCont As Boolean, blProtScen As Boolean, blProtDO As Boolean
Dim shp As Shape
Dim wbSource As Workbook
    fname = Application.GetOpenFilename( _
            FileFilter:="Excel檔(*.xls),*.txt", _
            Title:="請選取要減肥的檔案", MultiSelect:=False)
    '判斷使用者是否有選取檔案,或按取消
    If VarType(LogFileName) = vbBoolean Then
        Exit Sub
    End If
    Set wbSource = Application.Workbooks.Open(CStr(fname))
    fkb = FileLen(wbSource.FullName) / 1024
    On Error Resume Next
    For Each wksWks In wbSource.Worksheets
        Err.Clear
        'Store worksheet protection settings and unprotect if protected.
        blProtCont = wksWks.ProtectContents
        blProtDO = wksWks.ProtectDrawingObjects
        blProtScen = wksWks.ProtectScenarios
        wksWks.Unprotect ""
        If Err.Number = 1004 Then
            Err.Clear
            MsgBox "'" & wksWks.Name & _
                    "' 工作表有密碼保護無法檢查." _
                    , vbInformation
        Else
            Application.StatusBar = "檢查 " & wksWks.Name & ", 請稍候..."
            r = 0
            c = 0
            'Determine if the sheet contains both formulas and constants
            Set ur = Union(wksWks.UsedRange.SpecialCells(xlCellTypeConstants), _
                    wksWks.UsedRange.SpecialCells(xlCellTypeFormulas))
            'If both fails, try constants only
            If Err.Number = 1004 Then
                Err.Clear
                Set ur = wksWks.UsedRange.SpecialCells(xlCellTypeConstants)
            End If
            'If constants fails then set it to formulas
            If Err.Number = 1004 Then
                Err.Clear
                Set ur = wksWks.UsedRange.SpecialCells(xlCellTypeFormulas)
            End If
            'If there is still an error then the worksheet is empty
            If Err.Number <> 0 Then
                Err.Clear
                If wksWks.UsedRange.Address <> "$A$1" Then
                    ur.EntireRow.Delete
                Else
                    Set ur = Nothing
                End If
            End If
            'On Error GoTo 0
            If Not ur Is Nothing Then
                arCount = ur.Areas.Count
                For Each ar In ur.Areas
                    i = i + 1
                    tr = ar.Range("A1").Row + ar.Rows.Count - 1
                    tc = ar.Range("A1").Column + ar.Columns.Count - 1
                    If tc > c Then c = tc
                    If tr > r Then r = tr
                Next
                'Determine the area covered by shapes
                'so we don't remove shading behind shapes
                For Each shp In wksWks.Shapes
                    tr = shp.BottomRightCell.Row
                    tc = shp.BottomRightCell.Column
                    If tc > c Then c = tc
                    If tr > r Then r = tr
                Next
                Application.StatusBar = "清理 " & _
                        wksWks.Name & ", 工作表上多餘的格式,請稍候..."
                Set ur = wksWks.Rows(r + 1 & ":" & wksWks.Rows.Count)
                ur.Clear
                ur.EntireRow.RowHeight = _
                        wksWks.StandardHeight
                Set ur = wksWks.Range(wksWks.Cells(1, c + 1), _
                        wksWks.Cells(1, 256)).EntireColumn
                ur.EntireColumn.ColumnWidth = _
                        wksWks.StandardWidth
            End If
        End If
        'Reset protection.
        wksWks.Protect "", blProtDO, blProtCont, blProtScen
        Err.Clear
    Next
    DoEvents
    wbSource.Save
    lkb = FileLen(wbSource.FullName) / 1024
    MsgBox "'" & wbSource.Name & _
            "' 已減肥成功" & fkb - lkb & "KB", vbInformation
    wbSource.Close SaveChanges:=True
    Application.StatusBar = False
End Sub

File download

Code

檔案下載

Popularity: 42%

About the Author
    網路化名: chijanzen、中國龍、邪兵衛 經歷: 第二屆微軟社群之星 第三屆微軟「最有價值專家」 第五屆微軟「最有價值專家」
Leave a Comment »9 Comments
  • Reply » Avan 六月 22, 2010

    我瘦身2003的.xls檔案成功了!
    大家可以試試看!

  • Reply » KeySabreur 六月 2, 2010

    這檔案真好用,借轉貼到Facebook,謝謝。
     

  • Reply » ss 四月 8, 2010

    goog

  • Reply » KIRORO 一月 5, 2010

    chijanzen
    有針對2003版本的嗎?
    多謝啦!

  • Reply » chijanzen 十二月 2, 2009

    你好:
    按一下會走動的磁片圖示就能下載
    或由此下載
    http://chijanzen.net/file/H0072.rar

  • Reply » ttp 十二月 2, 2009

    對不起,
    沒看到給excel2003用的檔案...

  • Reply » chijanzen 五月 17, 2009

    啊!
    這檔案是eccel 2003的!你再試試

  • Reply » cncc 五月 12, 2009

    呵呵,下了才发现是2007用的,老师搞个2003的来用下哈。

Get a GravatarLeave a Reply

Name: « Required

Email Address: « Required

Website URL: « Optional

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About me
chijanzen 分享個人Excel VBA 學習經驗,架站心得, 日常生活記事等...

Add to Google

分類
Translator
Chinese (Simplified) flagItalian flagKorean flagEnglish flagGerman flag
French flagJapanese flagRussian flagBulgarian flagFinnish flag
相簿