Vba : Save Range of Cells as Image With Criteria

muhammad susanto

Well-known Member
Joined
Jan 8, 2013
Messages
2,089
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hi all...
i'm looking for vba code to save range of cells as image in Excel (.jpg or .png format)
with step or criteria like this:
1. first macro run in active sheet (any name sheet)
2. when i click run macro show message "please, select your range.."
3. then browse location Save in....
4. i don't use image for send email
i found code but i don't know use and modified
VBA Code:
Sub createJpg(SheetName As String, xRgAddrss As String, nameFile As String)
    Dim xRgPic As Range
    Dim xShape As Shape
    ThisWorkbook.Activate
    Worksheets(SheetName).Activate
    Set xRgPic = ThisWorkbook.Worksheets(SheetName).Range(xRgAddrss)
    xRgPic.CopyPicture
    With ThisWorkbook.Worksheets(SheetName).ChartObjects.Add(xRgPic.Left, xRgPic.Top, xRgPic.Width, xRgPic.Height)
        .Activate
        For Each xShape In ActiveSheet.Shapes
            xShape.Line.Visible = msoFalse
        Next
        .Chart.Paste
        .Chart.Export Environ$("temp") & "\" & nameFile & ".jpg", "JPG"
    End With
   Worksheets(SheetName).ChartObjects(Worksheets(SheetName).ChartObjects.Count).Delete
Set xRgPic = Nothing
End Sub
would you help me out greatly appreciated..

.susanto
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try the following macro. You'll notice that it exports the image as a PNG file, since it's more appropriate for screenshots. Change the default path and filename as desired.

VBA Code:
Option Explicit

Sub ExportRangeToPNG()

    On Error Resume Next
    Dim userRange As Range
    Set userRange = Application.InputBox(Prompt:="Select a range to export", Title:="Select Range", Type:=8)
    If userRange Is Nothing Then
        Exit Sub
    End If
    On Error GoTo 0
 
    Dim defaultPathAndFilename As String
    defaultPathAndFilename = Application.DefaultFilePath & "\" & Replace(userRange.Address(0, 0, xlA1, 1), ":", "to") & ".png"
 
    Dim saveAsFilename As Variant
    saveAsFilename = Application.GetSaveAsFilename(InitialFileName:=defaultPathAndFilename, FileFilter:="Portable Network Graphics (*.png), *.png", Title:="Save As")
 
    If saveAsFilename = False Then Exit Sub
 
    userRange.CopyPicture Appearance:=xlScreen, Format:=xlPicture
 
    With userRange.Parent.ChartObjects.Add(Left:=0, Top:=0, Width:=userRange.Width + 2, Height:=userRange.Height + 2)
        .Activate
        With .Chart
            .ChartArea.Format.Line.Visible = msoFalse
            .Paste
            With .Pictures(1)
                .Left = .Left + 2
                .Top = .Top + 2
            End With
            .Export CStr(saveAsFilename)
        End With
        .Delete
    End With
 
End Sub

Hope this helps!
 
Last edited:
Upvote 0
Solution
Hi Muhammad,

That's great, I'm glad I could help.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top