How to insert a picture into a cell using VBA in Excel

You can easily insert images into Excel spreadsheets and edit them with macro code. Here's how to insert a picture into a cell using VBA in Excel .

How to insert a picture into a cell using VBA in Excel

Visual Basic for Applications, abbreviated as VBA, is a form of Visual Basic 6 integrated into Microsoft Office programs. Through coding, VBA allows you to automate tasks in Office applications, including Excel. In some cases, you can even add new features to Excel using VBA.

Even though you need to work with code to use VBA, that doesn't mean VBA is full of letters and numbers. With VBA in Excel, you can create a macro that allows you to insert a picture into a cell or range of cells. Here's what you need to know about inserting pictures into cells in Excel using VBA.

How to insert pictures into cells using VBA in Excel

To create a macro that inserts a picture into an Excel cell using VBA, you really don't need any advanced Visual Basic knowledge. All you need to do is turn on Developer tools, create a macro, and paste the appropriate code. Of course, you can also insert images into Excel without using VBA. However, in this article, we will focus on VBA.

1. Turn on Developer tools

To use VBA in Excel, you need to enable Developer tools. This action will enable the Developer tab in the ribbon, which is disabled by default.

  1. Open Excel .
  2. Go to the File menu .
  3. Click Options at the bottom of the screen. The Excel Options window will appear.
  4. In Excel Options, go to the Customize Ribbon tab .
  5. In Main Tabs , check Developer .

How to insert a picture into a cell using VBA in Excel

Now Developer tools, including VBA access, are enabled. You don't need to do this every time you want to use VBA in Excel. Developer tools will always be enabled until you disable them.

2. Create macros and insert code

  1. In Excel, go to the Developer tab .
  2. In the Code section, select Macros .
  3. In the new window, enter your macro name in Macro name . The article will use insertPhotoMacro .
  4. Click Create .

How to insert a picture into a cell using VBA in Excel

After clicking Create, the VBA window will open and display the code for your macro. Currently, the code will consist of 2 lines: a Sub to start the macro and an End Sub to end it.

How to insert a picture into a cell using VBA in Excel

Add a little code to this macro. Add the following line of code between the two lines:

Dim photoNameAndPath As Variant
Dim photo As Picture
photoNameAndPath = Application.GetOpenFilename(Title:="Select Photo to Insert")
If photoNameAndPath = False Then Exit Sub
Set photo = ActiveSheet.Pictures.Insert(photoNameAndPath)
With photo
.Left = ActiveSheet.Range("A1").Left
.Top = ActiveSheet.Range("A1").Top
.Width = ActiveSheet.Range("A1").Width
.Height = ActiveSheet.Range("A1").Height
.Placement = 1
End With

The final code will look like this:

How to insert a picture into a cell using VBA in Excel

You don't need to worry about saving this process. Any changes you make in VBA are saved immediately.

Now it's time to see the code in action:

  1. Close the VBA window.
  2. Go to the Developer tab in Excel.
  3. Select Macros from the Code section.
  4. Highlight the macro you just created.
  5. Click Run .

Now a message will open asking you to locate the image file you want to insert. Select the photo, then click Open. You will now see the image in cell A1.

Note, Excel will shrink the image so it fits in cell A1. You can change it and change the code to insert images into other cells, even a range of cells. In the next section, we will separate the code and explain the parameters.

3. Split code

For VBA code to work the way you want, you need to understand it. When doing this, you can change the code to insert images into any cell at any size.

Sub insertPhotoMacro()
Dim photoNameAndPath As Variant
Dim photo As Picture
photoNameAndPath = Application.GetOpenFilename(Title:="Select Photo to Insert")
If photoNameAndPath = False Then Exit Sub
Set photo = ActiveSheet.Pictures.Insert(photoNameAndPath)
With photo
.Left = ActiveSheet.Range("A1").Left
.Top = ActiveSheet.Range("A1").Top
.Width = ActiveSheet.Range("A1").Width
.Height = ActiveSheet.Range("A1").Height
.Placement = 1
End With
End Sub

When code begins, the Dim statement is used to determine the variable type. We have two variable types: photoNameAndPath and photo . First the variable and then the image.

From there, the photoNameAndPath variable runs and it will open an application to get the location of the photo file. This is done via Application.GetOpenFileName . The Title parameter is optional. The content inside it is displayed as the window name.

Using If photoNameAndPath = False Then Exit Sub , if an invalid or empty address is given, the process will end. However, if a suitable file is provided, Set photo = ActiveSheet.Pictures.Insert(photoNameAndPath) indicates that the picture should be set as a variable as originally defined. It will be inserted into the active worksheet.

Finally, use With photo and the 5 lines after it to determine the location of the photo. .Left and .Top refer to the starting position, while .Width and .Height refer to the ending position. If you intend to insert images into another cell or a range, these are the lines you should change.

.Placement indicates that the picture should be resized to fit the cell or inserted freeform. Setting it to 1 will resize it according to the cell.

Finally, the above code uses End With , then End Sub to close the macro. Now you can change the variables photoNameAndPath and photo to any other name you like. Just remember to keep the names consistent throughout the code.

Above is how to use VBA to insert images into cells in Excel . Hope the article is useful to you.

Sign up and earn $1000 a day ⋙

How to pin comments on TikTok

How to pin comments on TikTok

How to pin comments on TikTok, TikTok allows users to pin comments easily. Here's how to pin comments on TikTok videos.

Instructions for Organizing a Q&A session on Facebook

Instructions for Organizing a Q&A session on Facebook

Instructions for Organizing a Q&A session on Facebook, Recently, Facebook has launched the feature to create a Q&A session, when you post a question for everyone to answer.

Instructions for installing and recording videos with Likee

Instructions for installing and recording videos with Likee

Instructions for installing and recording videos with Likee, Likee is an application that supports extremely beautiful and impressive video recording, and is loved by many young people today. The following,

How to export the list of meeting participants in Zoom

How to export the list of meeting participants in Zoom

How to export the list of meeting participants in Zoom, How to take attendance on Zoom is not difficult, it even allows you to export the list of students participating in the class.

How to change font on Xiaomi

How to change font on Xiaomi

How to change font on Xiaomi, Want to create a new image for Xiaomi device? Just follow the instructions below to change the font style and size above

Instructions for creating a mirror image effect on Picsart

Instructions for creating a mirror image effect on Picsart

Instructions for creating a mirror image effect on Picsart, Picsart is an application where users only need to perform a few simple steps to transform the photo.

How to add new fonts to Microsoft Word

How to add new fonts to Microsoft Word

How to add new fonts to Microsoft Word, Can't find the font you want to use for documents in Microsoft Word? Don't worry, you can install new fonts for Word according to these steps

How to turn off the computer screen and still keep the computer running

How to turn off the computer screen and still keep the computer running

How to turn off the computer screen and still have the computer running, How to turn off the PC screen and still have the computer running on Win 10/11? Very simple. Here's how to turn off the computer screen

How to fix facebook error logging in 3/2024

How to fix facebook error logging in 3/2024

To fix the “An unexpected error occurred” issue when logging in to Facebook, you can try the following steps

Cant access Facebook, Messenger and Instagram, how to fix it immediately

Cant access Facebook, Messenger and Instagram, how to fix it immediately

Users reported that their Facebook, Messenger and Instagram accounts were suddenly logged out and could not be accessed again, both on the app and web version.

Instructions on how to take photos on Tiktok are extremely simple

Instructions on how to take photos on Tiktok are extremely simple

Instructions on how to take photos on Tiktok are extremely simple. Tiktok is a famous application for recording and creating short videos on social networks. However, this application also

How to find and get Instagram links

How to find and get Instagram links

How to find and get Instagram links, Instagram is one of the most popular and easy-to-use social networking platforms today. However, because it is designed specifically for mobile,

Instructions for locking the mouse cursor when playing games on BlueStacks

Instructions for locking the mouse cursor when playing games on BlueStacks

Instructions for locking the mouse cursor when playing games on BlueStacks, Instructions for turning on/off the mouse cursor lock feature when playing games in the BlueStacks App Player emulator.

Instructions for installing and using Faceapp using an emulator on PC

Instructions for installing and using Faceapp using an emulator on PC

Instructions for installing and using Faceapp using an emulator on PC, Simple and easy instructions on how to install and use the Faceapp face editing app using an emulator on PC

Instructions for creating a cover photo group on Facebook

Instructions for creating a cover photo group on Facebook

Instructions for creating a cover photo group on Facebook, Creating a cover photo group is a feature that helps users set Facebook cover photos with many different images (maximum of 6 photos).

Mastering Technical Analysis with the Stochastic Indicator on Bubinga

Mastering Technical Analysis with the Stochastic Indicator on Bubinga

Learn how to effectively use the Stochastic Indicator for technical analysis on Bubinga. Explore advanced techniques to enhance your trading strategies

Instructions for adding angel wings to photos using PicsArt

Instructions for adding angel wings to photos using PicsArt

Instructions for adding angel wings to photos using PicsArt, PicsArt is a photo editing application used by many people today. Today WebTech360 will introduce the article

How to turn off the Just accessed notification on Zalo

How to turn off the Just accessed notification on Zalo

How to turn off the Just accessed notification on Zalo. This article WebTech360 will guide you to turn off the Just accessed notification under your Zalo chat name.

Instructions for blocking and unblocking accounts on TikTok

Instructions for blocking and unblocking accounts on TikTok

Instructions for blocking and unblocking accounts on TikTok. Similar to other social networking applications, TikTok also allows users to block someone's account

How to write a long status with a colorful background on Facebook

How to write a long status with a colorful background on Facebook

How to write a long status with a colorful background on Facebook, Today, WebTech360 will introduce some steps to post a long status with a colorful background on Facebook,