How to Insert a Picture Into a Cell Using VBA in Excel
If you’re an Excel user who loves to work with images and graphics, then you will definitely find the VBA macros in Excel to be a lifesaver. You can use VBA macros to insert images and graphics into your Excel worksheets without any hassle.
In this article, we will provide you with a step-by-step guide on how to insert a picture into a cell using VBA in Excel. You will find this guide to be practical and helpful, especially if you want to insert multiple images into your Excel workbook.
Step 1: Open Visual Basic Editor
The first step is to open VBA editor in Excel. You can do this by pressing the ALT + F11 key combination.
Step 2: Create a New Module
Once you have opened the VBA editor, click on the “Insert” tab and select “Module” from the list of options. A new module window will appear on the right-hand side of the screen.
Step 3: Add the VBA Code
In the new module window, place the following code:
Sub Insert_Picture()
Dim picRange As Range
Dim pic As Picture
‘ Get selected cell range
Set picRange = Application.InputBox(“Select cell to insert picture”, “Excel User Guide”, Type:=8)
If picRange Is Nothing Then Exit Sub
‘ Add the picture
Set pic = picRange.Parent.Pictures.Insert(Application.GetOpenFilename(“JPEG (*.jpg;*.jpeg),*.jpg;*.jpeg,PNG (*.png),*.png,GIF (*.gif),*.gif”))
With pic
‘ Resize to fit cell
.Top = picRange.Top
.Left = picRange.Left
.Width = picRange.Width
.Height = picRange.Height
End With
End Sub
Step 4: Save the Macro
Save the macro with the name “Insert_Picture” and close the VBA editor.
Step 5: Using the Macro
Now, open your Excel workbook and select a cell where you want to insert the picture. Then, go to the “Developer” tab and click on the “Macro” button.
Select the “Insert_Picture” macro from the list of macros and click on the “Run” button.
The macro will ask you to select the picture you want to insert, then it will automatically add it to the selected cell and resize it to fit the cell dimensions.
Conclusion
As you can see, inserting a picture into a cell using VBA in Excel is an easy task that can be done in just a few minutes. By following the steps outlined above, you will be able to insert pictures into your Excel worksheets with ease. Additionally, this method is a great way to save time, especially if you need to insert multiple images into your workbook.