Public TempA As Double
Public TempB As Double
Public Sub createJpg(SheetName As String, xRgAddrss As Range)
'creates temp JPG file of range (xRgAddrss) by creating temp chart
'uses current wb sheet (sheetname) to locate temp chart
'kills temp JPG file after UF display
Dim xRgPic As Range
Worksheets(SheetName).Activate
Set xRgPic = xRgAddrss
xRgPic.CopyPicture
TempA = xRgPic.Width
TempB = xRgPic.Height
With ThisWorkbook.Worksheets(SheetName).ChartObjects.Add(xRgPic.Left, xRgPic.Top, xRgPic.Width, xRgPic.Height)
.Activate
.Chart.Paste
.Chart.Export Environ$("temp") & "" & "TempChart.jpg", "JPG"
End With
Worksheets(SheetName).ChartObjects(Worksheets(SheetName).ChartObjects.Count).Delete
End Sub
Sub test()
Dim FilDir As Object
On Error GoTo ErFix
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'*** change file path to suit
Set FilDir = Workbooks.Open(ThisWorkbook.Path & "\" & "test.xlsm")
'createJpg(this wb sheet, range address to display)
Call createJpg("Sheet1", FilDir.Worksheets("Sheet2").Range("A1:o22"))
Workbooks(FilDir.Name).Close SaveChanges:=False
ErFix:
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Set FilDir = Nothing
If Err.Number <> 0 Then
On Error GoTo 0
MsgBox "Error"
Exit Sub
End If
UserForm1.Show
End Sub