Place Picture Excel Worksheet Range into User Form with VBA

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

Split Function

Export Pictures from Excel

Load Picture Function

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

2 thoughts on “Place Picture Excel Worksheet Range into User Form with VBA”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.