To set an image to a height measured in inches, assign its VBA Height property the result of Application.InchesToPoints. For example, pic.Height = Application.InchesToPoints(2) sets the picture to 2 inches high; Excel’s shape dimensions are stored in points, not inches.
Why Excel VBA Uses Points
Excel displays picture sizes in familiar units such as inches, but the VBA object model expresses a shape’s height and width in points. One point is 1/72 of an inch, so an image that is 2 inches high has a height of 144 points. You can do that arithmetic yourself, but Application.InchesToPoints makes the intended unit explicit and avoids having to convert every value manually.
The conversion function accepts a number of inches and returns the equivalent number of points. Its result is what you assign to the image’s Height property. The reverse conversion is available as Application.PointsToInches, which is useful if a macro needs to report a shape’s current height in inches rather than change it.
Excel can store shape dimensions with fractional point values. As a result, a requested height does not have to be a whole number of inches or points. For instance, 1.25 inches converts to 90 points. A value such as 0.75 inches converts to 54 points.
Set a Named Picture to a Specific Height
The most dependable approach is to identify the worksheet and the picture by name. Replace Sheet1 and Picture 1 with the actual worksheet tab name and shape name in your workbook. This example changes the selected object by name in the code, so it does not depend on which cell happens to be active.
Sub SetNamedPictureHeight()
Dim ws As Worksheet
Dim pic As Shape
' Change these names to match the workbook.
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set pic = ws.Shapes("Picture 1")
' Set the picture height to exactly 2 inches.
pic.Height = Application.InchesToPoints(2)
End Sub
In the Visual Basic Editor, place the procedure in a standard module, then run SetNamedPictureHeight while the workbook containing the code is open. In Excel for Windows, press Alt+F11 to open the editor; Alt+F8 opens the Macro dialog for running a macro. If the picture’s name is not obvious, select it and look at the Name Box to the left of the formula bar, or use the Selection Pane from the Home ribbon’s Find & Select menu. Shape names are often generated automatically, but they can be renamed to something descriptive.
Before running a macro, save a copy of the workbook. VBA macro actions cannot be reversed with Ctrl+Z, so a saved copy is the simplest way to recover from an unintended change. A workbook containing VBA code should be saved in a macro-enabled format such as .xlsm; saving it as a standard .xlsx file removes the VBA project.
Set the Height of the Currently Selected Picture
If you only need to resize one picture at a time, you can use the selected shape rather than hard-code its name. The following version expects a picture or other shape to be selected when you run it. It also checks that the selection exposes a shape range before trying to resize it.
Sub SetSelectedShapeHeight()
Dim selectedShapes As ShapeRange
' A cell selection does not have a ShapeRange.
On Error Resume Next
Set selectedShapes = Selection.ShapeRange
On Error GoTo 0
If selectedShapes Is Nothing Then
MsgBox "Select a picture or shape first."
Exit Sub
End If
' Apply a height of 1.5 inches to the first selected shape.
selectedShapes(1).Height = Application.InchesToPoints(1.5)
End Sub
This version sets the first shape in the selection to 1.5 inches. Selecting several objects at once does not make the procedure resize all of them; it deliberately addresses only item 1. That behavior is useful when a user may accidentally have selected multiple objects. If your goal is to resize every selected object, loop from 1 through selectedShapes.Count and set each item’s Height property.
What Happens to the Picture’s Width
Changing height does not always mean that width remains fixed. Excel shapes have a LockAspectRatio setting. When the aspect ratio is locked, resizing the height generally adjusts the width in proportion, keeping the picture from looking stretched or compressed. This is usually the right choice for photographs, logos, and illustrations.
If you specifically need a fixed height and fixed width, unlock the ratio before assigning both dimensions. For example, set pic.LockAspectRatio = msoFalse, then assign pic.Height and pic.Width separately, converting each inch value with Application.InchesToPoints. Unlocking the ratio can distort the image, so use it only when the content can tolerate that change. To preserve proportions while setting a target height, leave the ratio locked and let Excel calculate the matching width.
For example, suppose a photo is 4 inches high and 6 inches wide. Reducing its height to 2 inches with the aspect ratio locked also reduces its width to 3 inches. With the ratio unlocked, you could set it to 2 inches high and 6 inches wide, but the result would be visibly wider than the original proportions.
Resize Every Picture on a Worksheet
For a worksheet containing many inserted pictures, loop through the worksheet’s shapes and filter for picture types. The sample below changes regular pictures and linked pictures to 1.25 inches high, while leaving text boxes, charts, and other shapes alone.
Sub SetAllPictureHeights()
Dim ws As Worksheet
Dim shp As Shape
Set ws = ThisWorkbook.Worksheets("Sheet1")
' Resize pictures and linked pictures only.
For Each shp In ws.Shapes
If shp.Type = msoPicture Or shp.Type = msoLinkedPicture Then
shp.Height = Application.InchesToPoints(1.25)
End If
Next shp
End Sub
The loop processes shapes on the named worksheet, not every worksheet in the workbook. To apply it throughout a workbook, add an outer loop over ThisWorkbook.Worksheets and run the shape loop for each sheet. Be aware that grouped objects may be represented as a group shape; this simple filter does not separately inspect picture items inside a group. Similarly, charts and icons are not treated as ordinary inserted picture shapes by this filter.
When processing many images, consider whether they should all have the same height even if their proportions produce different widths. If a worksheet is arranged as a catalog, differing widths may affect the visual layout. You can use a consistent row height or a separate width rule, but setting both dimensions without considering aspect ratio can deform the images.
Positioning, Cells, and Displayed Size
A picture’s height is a shape dimension; it is not the same thing as the height of the row behind it. Setting an image to 1 inch does not automatically make its row 1 inch high, and changing a row’s height does not necessarily resize the image. If an image must sit neatly within a cell, adjust the row and column dimensions as needed, then position the picture over the intended cell.
Excel also provides a shape placement setting that governs how an object behaves when its underlying cells move or resize. The placement choices correspond to moving and sizing with cells, moving without sizing, or remaining independent of cells. That setting affects the relationship between the object and the grid; it does not change the meaning of Height. A macro may set placement separately if the workbook’s layout requires it.
Keep in mind that the size visible on screen depends on zoom and display scaling. A picture set to 2 inches remains 2 inches in the workbook’s shape dimensions even if it looks larger or smaller at a particular zoom level. For print output, page scaling and printer settings can also affect the final printed size. If physical print dimensions matter, check the print preview and page setup rather than relying only on how large the picture looks on the monitor.
Common Errors and How to Avoid Them
- Using inches directly:
pic.Height = 2assigns 2 points, not 2 inches. Convert the inch value withApplication.InchesToPoints. - Using the wrong object name: A misspelled sheet or shape name causes a runtime error. Confirm the tab name and the object’s name, including spaces.
- Running against the wrong workbook:
ThisWorkbookmeans the workbook that contains the macro.ActiveWorkbookmeans the workbook currently active in Excel, which may be different. Use the reference that matches your intended target. - Selecting a cell instead of a picture: A selection-based macro needs a shape selection. Use a named-shape macro if users may run it while a cell is active.
- Unexpected width changes: Check
LockAspectRatio. A locked ratio preserves image proportions; an unlocked ratio allows separate dimensions but can stretch the picture. - Expecting Ctrl+Z to restore the image: Excel’s undo history does not undo VBA macro changes. Save a copy before running a resizing macro.
One frequent misconception is that Excel VBA needs a special image-sizing function. It does not: the central operation is setting a shape’s Height property. The conversion method simply translates the human-friendly inch measurement into the points expected by that property.
Windows, Mac, and Excel for the Web
The examples are for desktop Excel and use standard Excel VBA object-model features. On Windows, Alt+F11 opens the Visual Basic Editor and Alt+F8 opens the Macro dialog. On Mac, use Option+F11 to open the Visual Basic Editor; on some keyboards you may need Fn as well. Mac keyboard shortcuts and menu placement can vary by keyboard and Excel release, so the Developer tab or Excel’s Tools menu can also provide access to Visual Basic and macros.
Excel for Mac supports VBA, though some Windows-specific VBA features and integrations are not available there. The worksheet, shape, and inch-to-point operations shown here are ordinary Excel object-model operations rather than Windows API calls. Excel for the web does not run VBA macros or provide the desktop Visual Basic Editor; open the file in desktop Excel to execute this code. The web app may display workbook content, but it is not a substitute for running the VBA procedure.
Macros may be blocked by Excel’s security settings, particularly for files downloaded from the internet or received by email. Only enable macros in files you trust. Do not lower macro security globally just to run an unfamiliar workbook; check the file’s source and follow your organization’s security policy.
Frequently Asked Questions
How do I set an image to 1 inch high in Excel VBA?
Use pic.Height = Application.InchesToPoints(1), where pic is a reference to the picture’s Shape object. The conversion returns 72 points, which is the VBA height value for 1 inch.
Is the Excel VBA Height property measured in inches?
No. A shape’s Height property is measured in points. Use Application.InchesToPoints when entering a target in inches, or Application.PointsToInches when converting an existing point value to inches.
Can I change the height without changing the width?
Yes, if you unlock the aspect ratio and set only the height, the width can remain unchanged. However, that may distort the image. With the ratio locked, Excel adjusts the width proportionally as the height changes.
How can I resize all pictures on a sheet?
Loop through that worksheet’s Shapes collection and set Height for shapes whose type is msoPicture or msoLinkedPicture. Filter by type so the macro does not also resize unrelated objects such as text boxes or charts.
Why does the picture look like the wrong size after I set its height?
Screen zoom changes its apparent size, and print scaling can change its physical size on paper. Confirm the shape’s actual height in Excel’s size controls, then inspect print preview if the printed dimensions are what matter.
Related
- Excel Turn Off Circular Reference Warning?
- Excel Circular Reference Warning: Causes and Fixes
- Absolute Reference in Excel Without F4: Manual Methods
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Add Comments in Excel VBA: Notes and Threaded CommentsTo add a cell note with Excel VBA, select the cell and run ActiveCell.AddComment "Check this value." in…
- Excel Macro Keyboard Shortcuts: Assign and Run MacrosIn Excel for Microsoft 365 on Windows, assign a macro shortcut by choosing Developer > Macros, selecting the…
- VBA Outlook Create Contact Group from ExcelYes. In classic Outlook for Windows, an Excel VBA macro can read email addresses from a worksheet, create…
- Outlook Macros: How to Enable Them Safely in Classic OutlookTo enable VBA macros in classic Outlook for Windows, open File > Options > Trust Center > Trust…
- VBA Range Array: Read and Write Cells EfficientlyA VBA range array is a Variant array that holds values from a worksheet range in memory, so…
- How to List All Files in a Folder Using Excel VBATo list files directly inside a folder in Excel for Microsoft 365 on Windows, run a VBA macro…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.