Reduce File Size

aabbasi

Board Regular
Joined
Mar 4, 2002
Messages
188
Hi:

Is there a way to reduce the Excel file size considerably? This excel file is huge with many sheets. I do not like to use Zip function.

Thank you
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Have you tried deleting all unused ranges? Typically this can be a big size culprit, especially if you format entire rows & columns.

HTH,

Smitty
 
Upvote 0
Put this macro in the Thisworkbook module. It resets all worksheets in a workbook to the actual used range. See if this reduces your file size.
Rich (BB code):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'From: http://www.contextures.com/xlfaqApp.html#Unused
' Re-set used range

Dim myLastRow As Long
Dim myLastCol As Long
Dim wks As Worksheet
Dim dummyRng As Range

For Each wks In ActiveWorkbook.Worksheets
  With wks
    myLastRow = 0
    myLastCol = 0
    Set dummyRng = .UsedRange
    On Error Resume Next
    myLastRow = _
      .Cells.Find("*", after:=.Cells(1), _
        LookIn:=xlFormulas, lookat:=xlWhole, _
        searchdirection:=xlPrevious, _
        searchorder:=xlByRows).Row
    myLastCol = _
      .Cells.Find("*", after:=.Cells(1), _
        LookIn:=xlFormulas, lookat:=xlWhole, _
        searchdirection:=xlPrevious, _
        searchorder:=xlByColumns).Column
    On Error GoTo 0

    If myLastRow * myLastCol = 0 Then
        .Columns.Delete
    Else
        .Range(.Cells(myLastRow + 1, 1), _
          .Cells(.Rows.Count, 1)).EntireRow.Delete
        .Range(.Cells(1, myLastCol + 1), _
          .Cells(1, .Columns.Count)).EntireColumn.Delete
    End If
  End With
Next wks

End Sub
HTH

Mike
 
Upvote 0
Thank you very much for the code.. I right clicked on one of the tab of sheet and paste dyour code, and saved the file but no cjange in size. How I am suppose to use this code. I believe I am not doing it right.

Thank you
 
Upvote 0
The code needs to go on the 'ThisWorkbook' module rather than a standard module - should in the listed in the project pane for your work book

Also have a search of the board on 'bloat' or 'file size'

HTH
 
Upvote 0
problems running this code

Your code for reducing the Excel file size looks great, and has regular application.

however, I installed this im ThisWorkbook as recommended, and when I go to close the workbook, I get an error message 1004: "Delete method of Range class failed".

On debugging, the line immediately after the Else statement below was highlighted.

I would appreciate any assistance you can provide, please.

Code:
If myLastRow * myLastCol = 0 Then
        .Columns.Delete
    Else
        .Range(.Cells(myLastRow + 1, 1), _
          .Cells(.Rows.Count, 1)).EntireRow.Delete
        .Range(.Cells(1, myLastCol + 1), _
          .Cells(1, .Columns.Count)).EntireColumn.Delete
    End If
 
Upvote 0
Found it!

The original source of the above code states it does not work if there are any merged cells in a worksheet.

I have modifed the beginning of the code to skip any worksheets with merged cells:

Rich (BB code):
 Private Sub Workbook_BeforeClose(Cancel As Boolean)
'From: http://www.contextures.com/xlfaqApp.html#Unused
' Re-set used range

Dim myLastRow As Long
Dim myLastCol As Long
Dim wks As Worksheet
Dim dummyRng As Range
Dim AnyMerged As Variant

For Each wks In ActiveWorkbook.Worksheets
  With wks
      AnyMerged = wks.UsedRange.MergeCells
      
      If AnyMerged = False Then
        myLastRow = 0
        myLastCol = 0
        Set dummyRng = .UsedRange
        On Error Resume Next
        myLastRow = _
          .Cells.Find("*", after:=.Cells(1), _
            LookIn:=xlFormulas, lookat:=xlWhole, _
            searchdirection:=xlPrevious, _
            searchorder:=xlByRows).Row
        myLastCol = _
          .Cells.Find("*", after:=.Cells(1), _
            LookIn:=xlFormulas, lookat:=xlWhole, _
            searchdirection:=xlPrevious, _
            searchorder:=xlByColumns).Column
        On Error GoTo 0
    
        If myLastRow * myLastCol = 0 Then
            .Columns.Delete
        Else
            .Range(.Cells(myLastRow + 1, 1), _
              .Cells(.Rows.Count, 1)).EntireRow.Delete
            .Range(.Cells(1, myLastCol + 1), _
              .Cells(1, .Columns.Count)).EntireColumn.Delete
        End If
    End If
  End With
Next wks

End Sub
 
Upvote 0
The code works great, but when my Worksheets & workbook is protected i.e. Tools | Protect | Protect Workbook with a password (password = 'xyz", (Worksheets password = 'xyz'), it gives an error. Can the following code be amended to work while sheets / workbook is protected.



Private Sub Workbook_BeforeClose(Cancel As Boolean)
'From: http://www.contextures.com/xlfaqApp.html#Unused
' Re-set used range

Dim myLastRow As Long
Dim myLastCol As Long
Dim wks As Worksheet
Dim dummyRng As Range
Dim AnyMerged As Variant

For Each wks In ActiveWorkbook.Worksheets
With wks
AnyMerged = wks.UsedRange.MergeCells

If AnyMerged = False Then
myLastRow = 0
myLastCol = 0
Set dummyRng = .UsedRange
On Error Resume Next
myLastRow = _
.Cells.Find("*", after:=.Cells(1), _
LookIn:=xlFormulas, lookat:=xlWhole, _
searchdirection:=xlPrevious, _
searchorder:=xlByRows).Row
myLastCol = _
.Cells.Find("*", after:=.Cells(1), _
LookIn:=xlFormulas, lookat:=xlWhole, _
searchdirection:=xlPrevious, _
searchorder:=xlByColumns).Column
On Error GoTo 0

If myLastRow * myLastCol = 0 Then
.Columns.Delete
Else
.Range(.Cells(myLastRow + 1, 1), _
.Cells(.Rows.Count, 1)).EntireRow.Delete
.Range(.Cells(1, myLastCol + 1), _
.Cells(1, .Columns.Count)).EntireColumn.Delete
End If
End If
End With
Next wks

End Sub


Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,215,330
Messages
6,124,308
Members
449,152
Latest member
PressEscape

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top