Skip to main content

Excel Macro: Compress All Pictures In Excel Worksheet

This macro compresses all pictures in the active worksheet of Excel to email size pixel 96 (ppi).

Compress All Pictures In Excel Worksheet

Sub CompressAllPictures()

    Dim wsh As Worksheet
    Dim shp As Shape

    'Set wsh = Worksheets("Sheet1") 'Specify a worksheet
    Set wsh = ThisWorkbook.ActiveSheet  'or use active worksheet
    Application.ScreenUpdating = False  'Disable Screen Updating to speed up macro
    For Each shp In wsh.Shapes
        SendKeys "%e", True 'E-mail(96ppi)
        'SendKeys "%w", True 'Web(150ppi)
        SendKeys "~", True
        Application.CommandBars.ExecuteMso "PicturesCompress"
    Next shp
    Application.ScreenUpdating = True

End Sub

Most VBA code should be placed in Standard Modules unless specified.

If you see a comment '------------------ Modules------------------ in the code header that means put the code in a Standard Module. For more information, learn this course: Where should I put the Excel VBA code?

The following steps teach you how to put VBA code into a Standard Module:

  1. Activate the Visual Basic Editor by pressing ALT + F11.
  2. Right-click the project/workbook name in the Project Window.
  3. Choose Insert -> Module.
  4. Type or paste the code in the newly created module. You will probably need to change the sheet name, the range address, and the save location.
  5. Click Run button on the Visual Basic Editor toolbar.
  6. For more information, learn this course: Programming with Excel VBA

Leave a comment

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

Format your code: <pre><code class="language-vba">place your code here</code></pre>

1 comment
  1. AA
    Amaresh Achar

    how to modify the code to do the same thing for all the worksheet of the workbook?