Friday, 27 May 2016

VBA Macro to Save the Excel Range as Image

VBA Macro to Export Excel Range as Image to Clipboard
Sub Save_ExcelRange_As_Image()

Application.DisplayAlerts = False

Dim MyRange As Range
Dim NewChart As Chart

    Set MyRange = ThisWorkbook.Names("MyRng").RefersToRange

    ThisWorkbook.Sheets.Add(After:=Sheets(Sheets.Count()),Type:=xlChart).Name = "MyChart"
            
        Set TgtChart = ThisWorkbook.Charts("MyChart")
        'ThisWorkbook.Sheets.Add(After:=Sheets("Data"),Type:=xlChart).Name = "MyChart"         
        'ActiveChart.SetSourceData Source:=Range("MyRng")

        MyRange.CopyPicture xlScreen, xlPicture
        TgtChart.Activate
        ActiveChart.Paste

        'ActiveChart.Delete
        'ActiveChart.ChartArea.Select
        'ActiveChart.PlotArea.Select

 'Formatting Copied Named Range on the Chart       
    Selection.ShapeRange.ScaleHeight 1.1, msoFalse, msoScaleFromBottomRight
    Selection.ShapeRange.IncrementLeft 30
    Selection.ShapeRange.IncrementTop 125

'Exporting/Saving the Chart as a .jpg Image 
TgtChart.Export FileName:="C:\Users\Tamatam\Desktop\Temp\MyRange.jpg", Filtername:="JPG"

TgtChart.Delete
MsgBox "Target Range Saved as JPG Image", vbOKOnly, "Job Over"

End Sub

No comments:

Post a Comment

Follow Me by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts