Cropping Graphics in a Macro

Written by Allen Wyatt (last updated May 20, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


John often pastes screen shots into an Excel workbook. He invariably needs to crop these graphics on all four sides by a set amount. Because he does this task so often, he wonders if there is a way to do it easily in a macro.

There are a number of ways you can address this problem, and all of them involve the use of macros. (There are no built-in commands to do such a task.) If you simply want to crop a graphic you've already added, you can do so by using a simple macro such as this:

Sub CropPicture()
    With Selection.ShapeRange.PictureFormat
        .CropLeft = 200
        .CropTop = 300
        .CropBottom = 50
        .CropRight = 100
    End With
End Sub

You would, of course, need to specify the values for the cropping. These values are specified in points, and they are relative to the original size of the picture. So, for instance, if you previously scaled the selected picture to 150% of its original size, and then you run this macro, all of the crop settings will appear to be 1.5 times their actual values. (So, for instance, the left cropping would appear to be 300 pixels, not 200 pixels.)

The macro could easily be modified so that it does the pasting and then automatically crops the image. This is done by simply adding a single-line paste command:

Sub PasteCropPicture()
    ActiveSheet.Paste
    With Selection.ShapeRange.PictureFormat
        .CropLeft = 200
        .CropTop = 300
        .CropBottom = 50
        .CropRight = 100
    End With
End Sub

The macro assumes that you previously copied the screen shot to the Clipboard; if you try to run it without having done so, you may well get an error.

There is one other thing to remember when running a macro such as this: When it is done, it will appear that your image has moved from its original position in the worksheet. This occurs because cropping from all four sides will change the apparent upper-left corner of the image to a different location. You can, if desired, compensate for this in your macro by adding code that "remembers" the upper-left corner of the image:

Sub CropPositionPicture()
    Dim MyShape As ShapeRange
    Dim LeftSide As Single
    Dim TopSide As Single

    Set MyShape = Selection.ShapeRange
    LeftSide = MyShape.Left
    TopSide = MyShape.Top

    With MyShape.PictureFormat
        .CropLeft = 200
        .CropTop = 300
        .CropBottom = 50
        .CropRight = 100
    End With

    MyShape.Left = LeftSide
    MyShape.Top = TopSide
End Sub

Of course, there is one thing to remember when you choose to crop graphics, regardless of how you choose to do so. Cropping doesn't affect image size. So, for instance, if you crop a screen shot so that only half of the screen is visible, all the "invisible" portions of the screen shot are still there, taking up space. This adds to the overall size of your workbook, sometimes amazingly so.

The only way around this is to not crop any graphics within Excel. Instead, process the image in a graphics program, save your processed image, and then insert that processed image in the worksheet. The result is smaller workbooks which means faster saving and loading.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11580) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Removing All Bookmarks

Need to get rid of a lot of bookmarks all at once? Word doesn't provide a way to do it, but you can use the short macro ...

Discover More

Self-Aware Macros

Sometimes it may be helpful for a macro to know exactly where it is being executed. This tip provides a way that you can ...

Discover More

Displaying the User Name in the Status Bar or Title Bar

Sometimes it can come in handy to know who the current computer user is, as far as Word is concerned. This tip presents ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Importing Based on a Partial File Name

A common task for macros is to open and process a file you want imported into your workbook. If you need to identify the ...

Discover More

Finding the Last-Used Cell in a Macro

Ever wonder what the macro-oriented equivalent of pressing Ctrl+End is? Here's the code and some caveats on using it.

Discover More

Delimited Text-to-Columns in a Macro

The Text-to-Columns tool is an extremely powerful feature that allows you to divide data in a variety of ways. Excel even ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two less than 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.