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 Access An Android Phone With A Broken Screen

How To Access An Android Phone With A Broken Screen

Dealing with a broken screen on your Android phone is a hassle. Although phone screens are pretty tough, one nasty drop can shatter them completely. Given that most people have a lot of irreplaceable content on their phones, it is

Find out how to find friends and family using online services

Find out how to find friends and family using online services

In today's world, where information is more accessible than ever before, finding long-lost friends or relatives has become much easier. Online services for finding people provide convenient and effective tools that allow you to restore lost connections.

IObit Uninstaller 13 - One Click to Uninstall All Unwanted Programs

IObit Uninstaller 13 - One Click to Uninstall  All Unwanted Programs

In the busy software utilities industry, the available uninstallation tools are often overshadowed by more glamorous alternatives. Nonetheless, an excellent way of removing programs on a PC is vital for optimal system performance and the elimination of unwanted applications.

Instructions for creating Moon phase trend

Instructions for creating Moon phase trend

Instructions for creating a Moon phase trend. The Moon trend is extremely popular on TikTok, creating an unprecedented fever. Through Moon phase will help you know the date

How to edit videos posted on YouTube without losing views

How to edit videos posted on YouTube without losing views

How to edit videos posted on YouTube without losing views, You forgot to blur sensitive content in videos posted on YouTube. Don't worry, here's how to adjust it

How to enable dark mode on TikTok

How to enable dark mode on TikTok

How to turn on dark mode on TikTok, How to turn on dark mode on TikTok is not difficult. The article will guide you in detail how to change the TikTok interface to black on Samsung

Shirt size chart - How to choose the most accurate shirt size

Shirt size chart - How to choose the most accurate shirt size

Shirt size chart - How to choose the most accurate shirt size, What is the shirt size chart? Shirt size means only the size and size of the shirt or pants. Shirt size varies according to direction

Instructions for drawing prizes to receive Lucky Money on ZaloPay

Instructions for drawing prizes to receive Lucky Money on ZaloPay

Instructions for drawing prizes to receive Lucky Money on ZaloPay. Recently, Zalo has launched the program "Amazing lucky money, Happy Spring Luck" taking place from January 29, 2021 to

How to view and recover Microsoft Outlook password

How to view and recover Microsoft Outlook password

How to view and recover Microsoft Outlook password, How to view Outlook password on computer is not difficult. This article will guide you how to view and recover Outlook password

Instructions for creating and searching for draft posts on Facebook

Instructions for creating and searching for draft posts on Facebook

Instructions for creating and searching for draft articles on Facebook. Currently, we can save articles that we have created but have not yet posted on Facebook. Here, WebTech360 invites

How to set up LibreOffice Writer to work like Microsoft Word

How to set up LibreOffice Writer to work like Microsoft Word

How to set up LibreOffice Writer to work like Microsoft Word, If you're planning to switch from Microsoft Word to LibreOffice Writer but want a similar experience

Instructions for turning off saving videos on TikTok are extremely simple

Instructions for turning off saving videos on TikTok are extremely simple

Instructions for turning off saving videos on TikTok are extremely simple, to help people block others from downloading and saving their videos simply and easily. So today, WebTech360

Instructions for creating genealogical and organizational charts on Google Sheets

Instructions for creating genealogical and organizational charts on Google Sheets

Instructions for creating genealogical charts and organizing them on Google Sheets. Google Sheets is an online office application that helps us easily edit online spreadsheets,

How to import and manage multiple email accounts in Gmail

How to import and manage multiple email accounts in Gmail

How to import and manage multiple email accounts in Gmail, Want to manage all emails in Gmail? Let's learn how to manage and add multiple accounts with WebTech360

Instructions for using Photoshop filters for beginners

Instructions for using Photoshop filters for beginners

Instructions for using Photoshop filters for beginners, Each filter in Photoshop has its own algorithm and uses. Below are instructions for using the internal filter

How to create automatic clicks on Macrorify

How to create automatic clicks on Macrorify

How to create automatic clicks on Macrorify, Macrorify helps you create automatic click tasks and detect images. In this article, let's find out how with WebTech360

Instructions for logging out of Instagram account remotely

Instructions for logging out of Instagram account remotely

Instructions for remotely logging out of your Instagram account. If you don't know how to remotely log out of your Instagram account, today WebTech360 would like to introduce

Instructions for remotely logging out of Tik Tok account

Instructions for remotely logging out of Tik Tok account

Instructions for remotely logging out of Tik Tok account. To help people quickly log out of Tik Tok accounts from other devices, today WebTech360

Instructions for blurring photos on B612

Instructions for blurring photos on B612

Instructions for blurring photos on B612. Recently, the B612 application has updated the image blur effect. Next, WebTech360 will introduce the article Instructions for blurring photos

How to fix the error of printing blank pages in Microsoft Excel

How to fix the error of printing blank pages in Microsoft Excel

How to fix the error of printing blank pages in Microsoft Excel. Why do some Excel documents not display data or contain blank pages when printed? Below are the reasons