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 ⋙

Leave a Comment

Instructions for creating a family account on Grab

Instructions for creating a family account on Grab

Grab Family Account is now available to users aged 18 and above. You can add up to 9 members to your Grab family account.

Instructions for getting group link on Telegram

Instructions for getting group link on Telegram

Each chat group on Telegram will have its own group link. When people click on the group, they will have the option to join the group on Telegram immediately.

Instructions to turn videos into beautiful wallpapers on Android phones

Instructions to turn videos into beautiful wallpapers on Android phones

Are you bored with static wallpapers on your phone? Try this method of turning videos into beautiful wallpapers for Android.

How to update iPhone to the latest iOS version

How to update iPhone to the latest iOS version

Updating your iPhone software (iOS) is a good idea. Updates are available for free from Apple. They apply the latest features and security fixes to your iPhone. Here's how to update your iPhone to the latest version of iOS.

Instructions for limiting iPhone speaker volume

Instructions for limiting iPhone speaker volume

iOS 18.2 brings an option to limit iPhone speaker volume. Users can adjust how loud their iPhone speakers can play songs, videos, and other media.

Instructions to turn off File Explorer thumbnails Windows 11

Instructions to turn off File Explorer thumbnails Windows 11

Due to personal privacy or some other reason, many people want to turn off Windows 11 File Explorer thumbnails. Below are instructions to turn off Windows 11 File Explorer thumbnails.

How to reset iPhone to restore factory settings, any iPhone can do it

How to reset iPhone to restore factory settings, any iPhone can do it

Resetting your iPhone is the easiest way to reset your iPhone settings to their defaults. Resetting your iPhone can help you fix a few simple problems. Here are the steps to restore your iPhone to factory settings.

How to create AI radio on YouTube Music

How to create AI radio on YouTube Music

The YouTube Music app features Ask for Music, which helps create AI-powered radio based on each person's custom commands.

Basic guide to using Blooket - an application that creates learning games

Basic guide to using Blooket - an application that creates learning games

Blooket.com is known as an extremely useful educational support platform. Let's explore how to use Blooket through this article with Quantrimang.

Instructions to unpin default folders Windows 11

Instructions to unpin default folders Windows 11

Windows 11 File Explorer includes a number of default folders pinned to the sidebar. If you do not need to use them, you can completely unpin the default folders in File Explorer.

How to enable Notification Cooldown on Android 16

How to enable Notification Cooldown on Android 16

Notification Cooldown Android will gradually reduce the volume and vibration intensity of notifications when you receive too many notifications from the same app at once.

TFT Frost Team Season 12, TFT Frost Build

TFT Frost Team Season 12, TFT Frost Build

DTCL season 12's Iceborn is the most special race and its mechanism is somewhat similar to Sub-Zero in Mortal Kombat.

ScratchJR Maze Game Design Tutorial (Final Post)

ScratchJR Maze Game Design Tutorial (Final Post)

For projects or games in ScratchJR, the start and end screens play an important role in enhancing the completeness of the product.

Instructions for adjusting the size of the Windows 11 scroll bar

Instructions for adjusting the size of the Windows 11 scroll bar

The scroll bar on Windows 11 is compact in size, but many people want to change this size, enlarge the scroll bar to make it easier to see when needed.

How to collaborate on notes in FaceTime

How to collaborate on notes in FaceTime

FaceTime iOS 17 has a collaboration feature in notes to type notes together and track changes as someone types.