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.

Leave a Comment

How to Fix Microsoft Edge Wallpaper Engine Crash Fix

How to Fix Microsoft Edge Wallpaper Engine Crash Fix

Tired of Microsoft Edge crashing with Wallpaper Engine? Get instant relief with our proven How to Fix Microsoft Edge "Wallpaper Engine" Crash guide. Step-by-step solutions for seamless wallpapers and browsing. No tech skills needed!

How to Set Up Microsoft Edge Parental Controls for 2026

How to Set Up Microsoft Edge Parental Controls for 2026

Master how to set up Microsoft Edge parental controls in 2026 with this step-by-step guide. Protect your children online using Family Safety features, content filters, and screen time limits for worry-free browsing.

How to Fix Microsoft Edge Audio Out of Sync Web Video

How to Fix Microsoft Edge Audio Out of Sync Web Video

Struggling with audio out of sync in Microsoft Edge web videos? Get instant fixes for perfect audio-video sync. Step-by-step guide with proven methods to resolve lag, delay, and desync issues on sites like YouTube and Netflix.

Solving Microsoft Edge Windows Hello PIN Reset Error

Solving Microsoft Edge Windows Hello PIN Reset Error

Tired of the frustrating Microsoft Edge "Windows Hello PIN" reset error blocking your browsing? Follow our proven, step-by-step fixes to resolve it fast and securely on the latest Windows versions. No tech expertise needed!

Solving Microsoft Edge Startup Logo Stuck Fix (2026)

Solving Microsoft Edge Startup Logo Stuck Fix (2026)

Tired of Microsoft Edge startup logo stuck freezing your browser? Get step-by-step fixes for 2026, from quick resets to advanced troubleshooting, and browse smoothly again. No tech skills needed!

Solving Microsoft Edge Sidebar Not Showing Glitch

Solving Microsoft Edge Sidebar Not Showing Glitch

Tired of the Microsoft Edge Sidebar Not Showing glitch? Get instant fixes with this step-by-step guide. Restore your sidebar for Copilot, shopping, and more – quick, easy, and effective solutions that work on the latest Edge versions.

Troubleshooting Microsoft Edge Context Menu Slow Fix

Troubleshooting Microsoft Edge Context Menu Slow Fix

Tired of Microsoft Edge context menu slow delays? This ultimate troubleshooting guide delivers proven fixes, quick resets, and advanced tips to restore lightning-fast right-click speed in Edge. Get started now!

Solving Microsoft Edge Status_Stack_Overflow Error

Solving Microsoft Edge Status_Stack_Overflow Error

Tired of Microsoft Edge crashing with "Status_Stack_Overflow" error? Follow our expert, step-by-step guide to solve the Status_Stack_Overflow error fast and restore smooth browsing. No tech skills needed!

Solving Microsoft Edge Unknown Hard Error on Laptop

Solving Microsoft Edge Unknown Hard Error on Laptop

Tired of Microsoft Edge crashing with "Unknown Hard Error" on your laptop? Follow our proven, step-by-step guide to fix this BSOD issue fast – no tech skills required! Get smooth browsing back today.

Troubleshooting Microsoft Edge Script Error in Outlook

Troubleshooting Microsoft Edge Script Error in Outlook

Struggling with Microsoft Edge "Script Error" in Outlook? Discover step-by-step troubleshooting fixes, from clearing cache to advanced resets, to resolve the issue fast and regain seamless email access. Updated with the latest solutions.

How to Recover Your DBD Progress After Account Migration

How to Recover Your DBD Progress After Account Migration

Stuck without your perks and levels after DBD account migration? Discover the ultimate step-by-step guide to recover your Dead by Daylight progress quickly and painlessly. Cross-progression fixed!

How to Stop Microsoft Edge from Running in the Background

How to Stop Microsoft Edge from Running in the Background

Learn exactly how to stop Microsoft Edge from running in the background with proven, step-by-step methods. End sneaky processes, free up RAM, and supercharge your PC performance effortlessly.

How to Disable Microsoft Edge Auto-Start on Windows 11

How to Disable Microsoft Edge Auto-Start on Windows 11

Learn how to disable Microsoft Edge auto-start on Windows 11 effortlessly. Boost your PC speed with step-by-step methods via Settings, Task Manager, and more. Quick fixes for faster boot times!

How to Delete Microsoft Edge from Mac and MacBook Safely

How to Delete Microsoft Edge from Mac and MacBook Safely

Discover the safest way to delete Microsoft Edge from Mac and MacBook. Step-by-step instructions to completely uninstall Edge, remove leftovers, and reclaim space without errors. Perfect for macOS users!

How to Change Microsoft Edge Browser Theme and Background

How to Change Microsoft Edge Browser Theme and Background

Discover simple steps to change Microsoft Edge browser theme and background. Personalize your browsing with custom themes, wallpapers, and pro tips for a fresh look.