We cannot place a picture of an Excel worksheet range directly as image into a user-form. First we create a picture of the required range either by using the simple copy-paste procedure or by taking a snapshot with the camera tool. This picture can now be placed into a chart area. We finally export the chart object as a GIF or JPG file which can be quickly and easily placed onto a user-form as shown in the training video below:
The complete VBA code to transfer a picture of an Excel worksheet range to a userform:
Private Sub CommandButton1_Click()
Range(“A1:B3”).Copy
Range(“R2″).Select
ActiveSheet.Pictures.Paste Link:=True
ActiveSheet.Pictures.Select
Application.CutCopyMode = False
Dim myChart As String, myPicture As String
Dim picWidth As Long, picHeight As Long
Application.ScreenUpdating = False
myPicture = Selection.Name
With Selection
picHeight = .ShapeRange.Height
picWidth = .ShapeRange.Width
End With
Charts.Add
ActiveChart.Location Where:=xlLocationAsObject, Name:=”Sheet1″
Selection.Border.LineStyle = 0
myChart = Selection.Name & ” ” & Split(ActiveChart.Name, ” “)(2)
With ActiveSheet
With .Shapes(myChart)
.Width = picWidth
.Height = picHeight
End With
.Shapes(myPicture).Copy
With ActiveChart
.ChartArea.Select
.Paste
End With
.ChartObjects(1).Chart.Export Filename:=”C:\testfolder\MyPic.jpg”, FilterName:=”jpg”
.Shapes(myChart).Cut
End With
Application.ScreenUpdating = True
Set Picture = LoadPicture(“C:\testfolder\MyPic.jpg”)
ActiveSheet.Pictures.Delete
End Sub
Further reading:
Enhance your Excel dashboard with the camera tool
Export pictures using Excel VBA
Pasting an image to a userform control