Save an Excel Cell Range as a JPG Using VBA
Question details
Export an Excel cell range as a JPG image and choose its file name and location each time.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A user wants to capture a range such as A1:AJ230 as an image for public sharing, using a VBA save dialog.
- Observed behavior
- The desired result is a JPG of the range saved to a user-selected location with a user-selected file name.
Open the workbook in desktop Excel and select the worksheet containing the range. Check that the range contains no information you do not want to share publicly.
Export the range through a temporary chart and save dialog
Use this macro to copy A1:AJ230 as a picture, export it as a JPG, and choose the destination each time.
The macro creates a temporary chart on the active worksheet, pastes the range picture into it, exports the chart, and deletes the chart. Change the range address in the code if needed.
In desktop Excel, open the Developer tab and select Visual Basic, then choose Insert > Module. If Developer is hidden, enable it in Excel's ribbon settings.
Paste this code into the module: Sub SaveRangeAsJPG() Dim ws As Worksheet Dim rng As Range Dim tempChart As ChartObject Dim fileName As Variant Dim exported As Boolean Dim errorMessage As String Set ws = ActiveSheet Set rng = ws.Range("A1:AJ230") fileName = Application.GetSaveAsFilename( _ InitialFileName:="ExcelRange.jpg", _ FileFilter:="JPEG Image (*.jpg), *.jpg") If VarType(fileName) = vbBoolean Then Exit Sub If LCase$(Right$(CStr(fileName), 4)) <> ".jpg" And _ LCase$(Right$(CStr(fileName), 5)) <> ".jpeg" Then fileName = CStr(fileName) & ".jpg" End If On Error GoTo HandleError rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture Set tempChart = ws.ChartObjects.Add( _ Left:=rng.Left, Top:=rng.Top, Width:=rng.Width, Height:=rng.Height) tempChart.Activate tempChart.Chart.Paste exported = tempChart.Chart.Export(FileName:=CStr(fileName), FilterName:="JPG") If Not exported Then errorMessage = "Excel could not export the JPG." GoTo CleanUp HandleError: errorMessage = Err.Description CleanUp: On Error Resume Next If Not tempChart Is Nothing Then tempChart.Delete On Error GoTo 0 If errorMessage <> "" Then MsgBox errorMessage, vbExclamation, "JPG export" Else MsgBox "Saved to " & CStr(fileName), vbInformation, "JPG export" End If End Sub
Save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Select the target worksheet, then choose Developer > Macros, select SaveRangeAsJPG, and click Run.
In the save dialog, select a folder, enter a file name, and click Save. Open the resulting JPG to check the capture before sharing it.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday spreadsheet work. The Excel VBA export macro above is specific to desktop Excel.

Frequently Asked Questions
How do I export a different range?
In the VBA editor, change ws.Range("A1:AJ230") to the range you want, such as ws.Range("B2:H30"), then run the macro from Developer > Macros.
Can I choose a new file name and folder every time?
Yes. The macro opens a Save As dialog each time it runs. Choose the folder and enter the image name before clicking Save.
Why should I save the workbook as an XLSM file?
The XLSM format retains VBA code. Use File > Save As and select Excel Macro-Enabled Workbook if you want to keep the macro in the workbook.
What happens if I cancel the save dialog?
The macro exits without creating the temporary chart or exporting a JPG.




