Streamline Your Excel Workbooks: Compress All Images in One Click

Streamline Your Excel Workbooks: Compress All Images in One Click

By Oren Sharon

Are your Excel workbooks ballooning in size because of high-resolution images? Manually compressing each picture can be tedious and time-consuming. With the power of VBA, you can automate the entire process—compress every image across all worksheets with a single click.

The VBA Macro Code

Copy the macro below and paste it into a new module in the VBA editor. Then sit back and watch Excel trim down your file size.

Sub CompressAllImagesInWorkbook()
    Dim ws As Worksheet
    Dim shCount As Long

    ' Turn off screen updates for speed
    Application.ScreenUpdating = False

    For Each ws In ActiveWorkbook.Worksheets
        shCount = ws.Shapes.Count
        If shCount > 0 Then
            ' Activate the sheet and select all shapes
            ws.Activate
            ws.Shapes.SelectAll

            ' Open the Compress Pictures dialog
            Application.CommandBars.ExecuteMso "PictureCompressDialog"

            ' Short pause to allow dialog to open
            Application.Wait Now + TimeValue("00:00:01")

            ' SendKeys to automate the dialog:
            ' Alt+A  → select "All pictures in document"
            ' Tab x2 → move to "Use document resolution"
            ' Alt+D  → select "Use document resolution"
            ' Enter  → OK
            SendKeys "%A{TAB 2}%D{ENTER}", True
        End If
    Next ws

    ' Restore screen updating
    Application.ScreenUpdating = True

    MsgBox "✅ All pictures in this workbook have been compressed.", vbInformation
End Sub

How It Works

  1. Loop Through Worksheets: The macro iterates over every sheet in your active workbook.
  2. Select All Shapes: It checks if a sheet contains any shapes (images/objects) and selects them.
  3. Invoke Compress Dialog: Using ExecuteMso, it calls Excel’s built-in Compress Pictures dialog.
  4. Automate with SendKeys: A brief pause ensures the dialog is ready, then SendKeys navigates the options to compress all images at the document resolution.
  5. Restore and Notify: Screen updating is turned back on and a confirmation message appears.

Step-by-Step Installation

  1. Open Excel and press Alt + F11 to launch the VBA editor.
  2. Right-click your workbook in the Project pane → Insert → Module.
  3. Paste the code above into the new module.
  4. Close the VBA editor. Back in Excel, go to Developer → Macros, select CompressAllImagesInWorkbook, and click Run.

Ready to Optimize Every Workbook?

If this macro saved you time, Subscribe to our newsletter for more Excel tips & VBA tricks! Don’t let large file sizes slow you down—empower your spreadsheets today.

#ExcelTips #VBA #ExcelVBA #SpreadsheetMagic #ExcelMacros #FileOptimization #OfficeHacks #Productivity #DataManagement #Automation






Leave a Reply

Your email address will not be published. Required fields are marked *