Excel VBA: Save a Range as a PNG Image File Automatically

VBA & macros · Updated November 19, 2026

To save an Excel range as an image with VBA, copy the range as a picture, paste it into a temporary chart, and export that chart as a PNG. The macro below does this for a named worksheet range and saves the image beside the workbook; Excel does not provide a direct Range.SaveAsImage method.

How the export works

The process uses two Excel features together. First, VBA calls the range’s CopyPicture method to place a visual copy of the cells on the clipboard. Then it creates a temporary chart, pastes the picture into the chart, and uses the chart’s Export method to write an image file. The chart is only a container for the picture; it is deleted after export.

This workaround is useful because copying a range as a picture and exporting a chart are supported Excel operations, while a range itself has no image-export method. The exported file is a snapshot, not a live connection: changing the cells later will not update an image that has already been saved.

The example uses PNG, a practical choice for tables, labels, and reports with text or sharp edges. It captures the displayed range, including its formatting, rather than saving the underlying cell values or formulas. The result is an image that can be inserted into a document, presentation, email, or web page.

Complete VBA macro for a PNG

In this example, the source is cells A1:F15 on a worksheet named Report. The output file is named Report.png and is saved in the same folder as the workbook. Change the sheet name and range to match your workbook.

Sub SaveRangeAsPNG()
    Dim sourceRange As Range
    Dim tempChart As ChartObject
    Dim outputPath As String
    Dim exportOK As Boolean
    Dim errorText As String

    On Error GoTo ExportError

    ' Set the worksheet and cells to capture.
    Set sourceRange = ThisWorkbook.Worksheets("Report").Range("A1:F15")

    ' A saved workbook provides the folder for the image.
    If Len(ThisWorkbook.Path) = 0 Then
        MsgBox "Save the workbook before exporting the image.", vbExclamation
        Exit Sub
    End If
    outputPath = ThisWorkbook.Path & Application.PathSeparator & "Report.png"

    ' Make a temporary chart the same size as the range.
    Set tempChart = sourceRange.Worksheet.ChartObjects.Add( _
        Left:=sourceRange.Left, Top:=sourceRange.Top, _
        Width:=sourceRange.Width, Height:=sourceRange.Height)

    ' Copy the range as a picture, paste it into the chart, and export.
    sourceRange.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    tempChart.Activate
    tempChart.Chart.Paste
    exportOK = tempChart.Chart.Export(Filename:=outputPath, FilterName:="PNG")

    If Not exportOK Then Err.Raise vbObjectError + 1000, , "Excel could not export the chart."

    tempChart.Delete
    Set tempChart = Nothing
    Application.CutCopyMode = False
    MsgBox "Image saved to: " & outputPath, vbInformation
    Exit Sub

ExportError:
    errorText = Err.Description
    On Error Resume Next
    If Not tempChart Is Nothing Then tempChart.Delete
    Application.CutCopyMode = False
    On Error GoTo 0
    MsgBox "PNG export failed: " & errorText, vbExclamation
End Sub

The error handler attempts to remove the temporary chart if a step fails, rather than leaving it in the worksheet. The macro also checks whether the workbook has been saved, because an unsaved workbook has no folder path to use for the output. If the export fails, the message box reports the error Excel returned; the clipboard or chart paste is a common source of trouble.

How to add and run the macro

  1. Save a copy of the workbook before running a macro. VBA changes cannot be undone with Ctrl+Z, and a macro can modify or remove workbook content.
  2. In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor. Choose Insert > Module, then paste the code into the new standard module.
  3. Replace Report and A1:F15 with the actual worksheet name and range. Worksheet names must match, including spaces.
  4. Save the workbook as an Excel Macro-Enabled Workbook (*.xlsm) if you want to keep the VBA code in it. Return to Excel and run SaveRangeAsPNG from Developer > Macros, or press Alt+F8, select the macro, and choose Run.
  5. If Excel displays a macro security warning, only enable macros for a workbook you trust. The macro writes an image file to the workbook’s folder.

On a Mac, desktop Excel also supports VBA, but the Visual Basic Editor shortcut differs by keyboard and settings; use Tools > Macro > Visual Basic Editor if the shortcut is unavailable. The code uses Application.PathSeparator to build the file path, but clipboard and chart-export behavior can vary between platforms. Excel for the web cannot run VBA macros, so use desktop Excel to perform this export.

Choose the range and output filename

The range can be any contiguous block of cells that Excel can copy as a picture. To capture a different area, change the address in Range("A1:F15"). For example, use Range("B2:H20") to capture seven columns and nineteen rows. If the range is on another sheet, put that sheet’s exact name in the worksheet reference.

The output path is assembled from the workbook’s folder, a path separator, and the filename. Change Report.png to a descriptive name such as MonthlySummary.png. If you run the macro repeatedly with the same filename, the new export may replace the earlier image. Use a different name for each version if you need to retain previous snapshots.

For a range that changes size, you can determine the address dynamically rather than hard-coding the last row. For instance, a report that always begins in A1 and has no blank rows or columns in its data might use the worksheet’s CurrentRegion property. That property expands to the connected block around A1, so it can include extra cells if the layout is not isolated. Check the range logic before automating a recurring export.

You can also replace the fixed filename with a file-picker dialog if users need to choose where to save each image. That requires adding dialog-handling code and checking that the user did not cancel. For a scheduled or repeatable report, a consistent folder and filename are usually simpler to maintain.

Image appearance and file formats

The macro uses Appearance:=xlScreen, which asks Excel to copy the range as it appears on screen, and Format:=xlPicture, which requests a picture representation for the clipboard. The chart is then exported as PNG. The chart export step determines the file type, so the extension and FilterName should agree: for example, use a .png filename with "PNG".

PNG generally preserves text and cell borders cleanly and is suitable when you need a broadly compatible image. JPEG is another possible export format, but its compression can soften small text and fine grid lines. GIF is limited in color handling and is rarely the best choice for a detailed spreadsheet. If you change the export format, use the corresponding supported filter name and a matching extension, and check the resulting image in the application where it will be used.

Image dimensions depend on the range’s width and height in Excel and on how the copied picture is rendered. A wider or taller range makes a larger image, but the VBA call does not provide a simple DPI setting. If an image looks too small in a document, resizing it there can reduce legibility; consider narrowing the selected range, adjusting column widths, or using a vector-friendly workflow when the destination supports one.

Hidden rows and columns, zoom, themes, conditional formatting, and screen rendering can affect what the copied picture looks like. For a print-oriented appearance, CopyPicture also supports Appearance:=xlPrinter, which uses printer-style rendering. That option can depend on installed printers and page setup, so compare the output before relying on it for unattended exports.

Common problems and fixes

  • The macro says the sheet is missing: Confirm the worksheet tab’s name exactly matches the string in Worksheets("Report"). A renamed tab or trailing space will cause an error.
  • No image appears, or the paste fails: Excel’s clipboard may not be ready when the next command runs, or another application may interfere. Try running the macro again, close dialogs, and avoid copying other content during the export. If the workbook is large or the machine is busy, a short wait after CopyPicture may help, but it does not cure every clipboard issue.
  • The image is blank or incomplete: Confirm that the selected range contains visible cells and that the chart paste succeeded. Test with a small, simple range first. Merged cells, hidden content, and unusual objects can make the copied result differ from the worksheet view.
  • The file is saved somewhere unexpected: The example writes to ThisWorkbook.Path, the folder containing the workbook that holds the macro. Save the workbook first and check that folder. It does not automatically use the folder of another workbook that happens to be active.
  • The macro reports an export error: Check that the destination folder is writable, the filename is valid, and the image is not open in a program that prevents replacement. Also verify that the chart received the pasted picture before Export is called.
  • A temporary chart remains behind: If Excel is interrupted or closed during execution, cleanup code may not run. Remove any leftover temporary chart manually, then run the macro again. The example’s error handler handles ordinary VBA errors but cannot guarantee cleanup after Excel itself stops responding.

Safety and compatibility notes

VBA macros can perform actions that ordinary worksheet formulas cannot, which is why Excel may block them or show a security prompt. Use code from a trusted source, and avoid enabling macros in unexpected files. This example exports a picture to the workbook’s folder and briefly creates a chart on the source worksheet; it does not need to change cell values.

Keep a backup before adapting the code, especially if you add steps that clear ranges, delete sheets, or overwrite files. Ctrl+Z does not reverse macro actions. The code is intended for desktop Excel, including Microsoft 365 for Windows; VBA is unavailable in Excel for the web. Mac desktop Excel has VBA, but differences in clipboard handling or export behavior mean it is sensible to test the macro on the target platform and Excel version.

Alternatives to VBA

For a one-time image, select the cells and use Home > Copy > Copy as Picture, then paste the result into an application that can save images. The available ribbon layout can vary with window size, but Copy as Picture is the relevant Excel command. This manual approach avoids adding macro code, though it does not automatically create a PNG file in a chosen folder.

If the range is part of a chart, export the chart itself with VBA rather than copying a cell range. If you only need to share the data, sending the workbook or exporting a PDF may be more useful than an image: images are static, cannot be filtered, and are less accessible than actual cell data. Choose PNG when a visual snapshot is the requirement, not as a substitute for editable spreadsheet content.

Frequently Asked Questions

Can VBA save a range directly as a PNG?
No. A Range object does not have a direct image-save method. The common VBA approach is to copy the range as a picture, paste it into a temporary chart, and export the chart as PNG.

Where does this macro save the image?
It saves the file in the same folder as the workbook containing the macro, using the name Report.png. The workbook must already be saved. Change the output path in the code to choose another folder or filename.

Can I save only visible cells?
The example copies the specified range as a picture, so hidden rows, filtered data, and other display conditions can affect the result. Test the exact range and filter state you intend to capture. If you need a strict visible-cells-only workflow, explicitly select or construct that range and verify the exported image.

Can I use this macro in Excel for the web?
No. Excel for the web does not run VBA. Open the workbook in desktop Excel for Windows or Mac to run the macro, or use the manual Copy as Picture workflow in a desktop version.

Will the saved image update when the cells change?
No. The PNG is a static snapshot taken when the macro runs. Run the macro again to create a refreshed image; use a new filename if you want to preserve the earlier version.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.