Excel VBA: Create a UserForm Image from a Worksheet – 3 Examples

 

Example 1 – Using the Copy Pasting Tool

You have an image in your worksheet. To add this image to the UserForm dialog box:

Using the Simple Copy Paste Tool

 

  • Go to the Developer tab and click Visual Basic (or press ALT + F11).

Using the Simple Copy Paste Tool

  • Select Insert and choose UserForm.

Using the Simple Copy Paste Tool

The dialog box UserForm1 will be displayed.

Using the Simple Copy Paste Tool

Note: You’ll see a floating window: The Toolbox. If it isn’t displayed, go to the View tab and choose Toolbox.

Using the Simple Copy Paste Tool

 

  • Choose Image in Toolbox and drag to draw a frame for the image.

Using the Simple Copy Paste Tool

  • Right-click while keeping the cursor over the image.
  • In the options, choose Properties.

Using the Simple Copy Paste Tool

  • Go to the worksheet again and copy the image by pressing CTRL + C.

Using the Simple Copy Paste Tool

  • Go to the UserForm1 Properties of Image1 and select None in Picture.
  • Paste the image by pressing CTRL + V.

Using the Simple Copy Paste Tool

This is the output.

Using the Simple Copy Paste Tool


Example 2 – Create a Worksheet Range Directly as Image in the UserForm

This is the sample dataset.

Excel VBA Userform Image from Worksheet Create Worksheet Range Directly as Image to UserForm

Step 1: Creating a Button

  • Go to the Developer tab and click Visual Basic (or press ALT + F11).
  • Create a new UserForm as shown in the first example: UserForm2.
  • Select Insert.
  • Insert a Button in Form Controls.

Excel VBA Userform Image from Worksheet Create Worksheet Range Directly as Image to UserForm

Name Button1 as Open UserForm.

Excel VBA Userform Image from Worksheet Create Worksheet Range Directly as Image to UserForm

  • Right-click the button and choose Assign Macro.

Excel VBA Userform Image from Worksheet Create Worksheet Range Directly as Image to UserForm

  • Enter the following code into the module to show UserForm2.
Sub Button1_Click()
UserForm2.Show
End Sub

Excel VBA Userform Image from Worksheet Create Worksheet Range Directly as Image to UserForm

Step 2: Adding a Command Button

Name the button Generate Picture.

Excel VBA Userform Image from Worksheet Create Worksheet Range Directly as Image to UserForm

Step 3: Assigning a VBA Code

  • Double-click the Generate Picture button and enter the following code.
Private Sub CommandButton1_Click()
Range("B3:D12").Copy 'Choose your Worksheet Range
Range("H4").Select 'Specify the Location of Keeping Picture Temporarily
ActiveSheet.Pictures.Paste Link:=True
ActiveSheet.Pictures.Select
Application.CutCopyMode = False
Dim tChart As String, tPicture As String
Dim imgWidth As Long, imgHeight As Long
Application.ScreenUpdating = False
tPicture = Selection.Name
With Selection
imgHeight = .ShapeRange.Height
imgWidth = .ShapeRange.Width
End With
Charts.Add
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet2" 'Type your Sheet Name
Selection.Border.LineStyle = 0
tChart = Selection.Name & " " & Split(ActiveChart.Name, " ")(2)
With ActiveSheet
With .Shapes(tChart)
.Width = imgWidth
.Height = imgHeight
End With
.Shapes(tPicture).Copy
With ActiveChart
.ChartArea.Select
.Paste
End With
.ChartObjects(1).Chart.Export Filename:="aC:\Exceldemy\MyPic.jpg", FilterName:="jpg"
.Shapes(tChart).Cut
End With
Application.ScreenUpdating = True
Set Picture = LoadPicture("C:\Exceldemy\MyPic.jpg")
ActiveSheet.Pictures.Delete
End Sub

Excel VBA Userform Image from Worksheet Create Worksheet Range Directly as Image to UserForm

In the above code:

  • Choose your worksheet range ( B3:D12).
  • Specify the location of the picture (H4).
  • Enter the sheet (Sheet2) from which you want to add an image.
  • Define a location (C:\Exceldemy\MyPic.jpg) to store and load the image.

 

  • Go to the worksheet and click Open UserForm.

Excel VBA Userform Image from Worksheet Create Worksheet Range Directly as Image to UserForm

The UserForm2 dialog box is displayed.

  • Click Generate Image.

Excel VBA Userform Image from Worksheet Create Worksheet Range Directly as Image to UserForm

This is the output.

Excel VBA Userform Image from Worksheet Create Worksheet Range Directly as Image to UserForm


Example 3 – Add a Worksheet Image to the UserForm

Create a chart from the following dataset to display sales variation.

Excel VBA Userform Image from Worksheet Add Worksheet Image to UserForm

This is the output:

How to Create Chart

  • Save the chart as an image by right-clicking, and choosing Save as Picture. Here, Chart1 in JPG format.
  • Add a command button in the Toolbox and name it Add Image.

Excel VBA Userform Image from Worksheet Add Worksheet Image to UserForm

  • Double-click the button and enter the following VBA code.
Private Sub CommandButton1_Click()
 Set Pic = UserForm3.Controls.Add("Forms.Image.1")
    
    With Pic
        'Load Chart as a Picture to Image Control
        .Picture = LoadPicture("C:\Exceldemy\Chart1.jpg")
        
        'Align the Picture Size
        .PictureSizeMode = fmPictureSizeModeZoom
        
        .Left = 70
        .Top = 20
        
    End With
End Sub

Excel VBA Userform Image from Worksheet Add Worksheet Image to UserForm

In the above code, the Add method is set with the UserForm object for the Pic variable. The LoadPicture method is used to add the image and the PictureSizeMode property to resize it. Change the location of the File Name using the LoadPicture method.

  • Run the code, (press F5), to display the UserForm3 dialog box.
  • Click Add Image.

Excel VBA Userform Image from Worksheet Add Worksheet Image to UserForm


Things to Remember

  • Image Control supports 6 specific image formats only :.bmp, .cur, .jpg, .ico, .wmf and .gif.
  • To use the LoadPicture method, you need ActiveX Controls.

Download Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Md. Abdul Kader
Md. Abdul Kader

MD. ABDUL KADER is an engineer with a talent for Excel and a passion for VBA programming. To him, programming is like a time-saving wizard, making data manipulation, file handling, and internet interactions a breeze. His skill set extends to SWM, GIS, RS, and SPSS. He holds a B.Sc in Urban & Regional Planning from Chittagong University of Engineering and Technology and has shifted to become a content developer. In this role, he crafts technical content centred around... Read Full Bio

4 Comments
  1. Hi Mr Kader,

    Thanks for sharing your valuable knowledge.

    Please be aware that there are other more advanced techniques that makes possible to add special functions or features to EXCEL like using Windows API functions, Excel Add-ins, external DLL libraries, etc.

    • Hello FRANK,

      Glad to know the basic methods were useful to you. We further appreciate your valuable insights in addition to these methods. We will look into the advanced techniques and add them in the future. Thank you.

      Regards,
      Yousuf Khan

  2. Hello,

    Could you please share the file of test ?

    I tried the code and it doesn’t work on my laptop. There are error message.

    It will be very usefull to get your test files.

    Thank you for your support and feedback


    Damien

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo