Image is one of the UserForm control. You can select and drag Image on the UserForm. You can select and drag Image on the UserForm. Image control embeds a picture such as a jpg, jpeg, gif, png, bitmap, etc. It can be used on the UserForm. You can see how it works and more details about Image Control.
VBA Image_Control on the UserForm
Please find more details about VBA ActiveX Image_Control on the UserForm.
-
- Go To Developer Tab and then click Visual Basic from the Code or Press Alt+F11.
- Go To Insert Menu, Click UserForm. Please find the screenshot for the same.
-
- Drag the Image_control on the Userform from the Toolbox. Please find the screenshot for the same.
-
- Click on the image_control properties.
- On the left side find ‘Picture’ from the available properties of the control.
- On the right side click on that, and select image from the source.
- On the left side find ‘PictureSizeMode’ from the available properties of the control.
- On the right side, select ‘1 – frmPictureSiseModeStretch’ from the available list.
- Now, Click ‘F5’ to see the output.
- Now, you can see the following output as shown below in the screen shot.
Add dynamic Image_Control on the UserForm using VBA
Please find the following steps and example code, it will show you how to add dynamic Image_control on the userform.
-
- Add Image and CommandButton on the userform from the toolbox.
- Right click on the CommandButton, click properties
- Change the CommandButton caption to ‘Create_Image ’
- Double click on the CommandButton
- Now, it shows the following code.
Private Sub CommandButton1_Click() End Sub
-
- Call the below procedure named ‘Add_Dynamic_Image ’ and find the below procedure to run.
Private Sub CommandButton1_Click() Call Add_Dynamic_Image End Sub
Procedure to call in the CommandButton:
Sub Add_Dynamic_Image() 'Add Dynamic Image and assign it to object 'Img' Set Img = UserForm2.Controls.Add("Forms.Image.1") With Img 'Load Picture to Image Control .Picture = LoadPicture("C:Image Excel ActiveX Control Object.jpg") ‘Change Image Path here 'Align the Picture Size .PictureSizeMode = fmPictureSizeModeStretch 'Image Position .Left = 50 .Top = 10 End With End Sub
-
- Now, click F5 to run the macro, click ‘Create_Image ’ button to see the result.
- You can see the created dynamic Image_control which is shown in the following screen shot.
output:
Delete Image_Control on the UserForm using VBA
Please find the below code, it will show you how to delete or remove the control on the UserForm. In the below example, its deleting the Image named ‘New Image’ which is on the UserForm named ‘UserForm4’. We can use Remove method to delete the controls which are created during run time. Controls which are created during design time cannot be deleted using this method. Please find the below example and screen shots for better understand.
Code 1: Adding control During Run Time
Private Sub CommandButton1_Click() 'We can use Add method to add the new controls on run time Set lblBtn = Me.Controls.Add("Forms.Image.1") With lblBtn .Top = 20 .Left = 40 .Name = "lblNew1" End With MsgBox "New Image Control Added" End Sub
Please find the below screen shot for your reference for the above macro and its output.
When we click on Add Command Button:
Code 1: Deleting or Removing Image_control which is created during run time.
Private Sub CommandButton2_Click() 'We can use Remove method to delete the controls which are created during run time 'Note: Controls which are created on design time cannot be deleted using this method Me.Controls.Remove ("lblNew1") MsgBox "Image Control Deleted" End Sub
Please find the below screen shot for your reference for the above macro and its output.
When we click on Delete Command Button: