How to Insert Pictures to Automatically Fit the Cell Size with VBA in Excel – 3 Steps

The sample dataset contains Company names and a column to insert their Logo.

insert pictures in excel automatically size to fit cells vba

 


Step 1 – Insert a Module

  • Go to the Developer tab.
  • Click Visual Basic.

Insert Module

In the Microsoft Visual Basic for Applications window:

  • Go to the Insert tab.
  • Select Module.

Inserting Module to insert picture automatically size to cells

 


Step 2 – Enter the VBA Code

  • Enter the following code in Module1.
Sub Insert_Automatically()
Dim Ph_Path As Variant
Dim Ph As Picture
Ph_Path = Application.GetOpenFilename(Title:="Select Your Desired Photo")
If Ph_Path = False Then Exit Sub
Set Ph = ActiveSheet.Pictures.Insert(Ph_Path)
With Ph
.Width = ActiveCell.Width
.Height = ActiveCell.Height
.Placement = 1
End With
End Sub

Code Breakdown

  • The sub-procedure is Insert_Automatically.
  • Two variables are declared.
  • The GetOpenFilename method is used to select the file.
  • The width and height of the picture are set to the size of the selected cell.

Write VBA Code to insert pictures automatically size to fit cells in Excel


Step 3 – Run the Macro

  • Select C5 to place the logo of Softeko.
  • Go to the Developer tab.
  • Click Macros in Code. You can also press ALT+F8.

Run the Macro

In the Macro dialog box:

  • Select the Insert_Automatically macro in Macro name:.
  • Click Run.

macro dialog box to run the vba code in excel

  • In the Select Your Desired Photo window, choose the image.
  • Click Open.

The logo is displayed in C5.

inserting pictures automatically fit to cells size in excel

  • Follow the same steps to insert the other images.

Procedure to Insert Pictures Automatically Size to Fit Cells Using VBA in Excel


How to Insert Pictures Manually to Fit Cells in Excel

Steps:

  • Select the cell to insert the logo. Here, C5.
  • Go to the Insert tab.
  • Click Illustrations.
  • Select Pictures.
  • Choose This Device in Insert Picture From.

How to Insert Pictures Manually to Fit Cells in Excel

  • In the Insert Picture dialog box, select the image.
  • Click Insert.

The logo is displayed but doesn’t fit the cell.

source pictures

To resize it:

  • Open the Visual Basic Editor and insert another module.
  • Copy the following code into Module2.
Public Sub Size_Fit_Cell()
On Error GoTo Select_Image
Dim ZImageWtoHRatio As Single
Dim QWtoHRatio As Single
With Selection
ZImageWtoHRatio = .Width / .Height
End With
With Selection.TopLeftCell
QWtoHRatio = .Width / .RowHeight
End With
Select Case ZImageWtoHRatio / QWtoHRatio
Case Is > 1
With Selection
.Width = .TopLeftCell.Width
.Height = .Width / ZImageWtoHRatio
End With
Case Else
With Selection
.Height = .TopLeftCell.RowHeight
.Width = .Height * ZImageWtoHRatio
End With
End Select
With Selection
.Top = .TopLeftCell.Top
.Left = .TopLeftCell.Left
End With
Exit Sub
Select_Image:
MsgBox "Choose an Image and Run the Macro."
End Sub

  • Save the Excel file.

To run this code:

  • Click the picture.
  • Go to the Developer tab.
  • Click Macros.

  • Select the Size_Fit_Cell macro and click Run.

Insert Pictures Manually to Fit Cells size in Excel

This is the output.

  • Follow the same steps to insert the logos in the other cells.

How to Insert Pictures Manually to Fit Cells size in Excel


Practice Section

Practice here.


Download Practice Workbook

Download the following Excel workbook.


 

Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Shahriar Abrar Rafid
Shahriar Abrar Rafid

Shahriar Abrar Rafid, BSc, Naval Architecture and Marine Engineering, Bangladesh University of Engineering and Technology, Bangladesh, has worked with the ExcelDemy project for more than 1 year. He has written over 100+ articles for ExcelDemy. He is a professional visual content developer adept at crafting scripts, meticulously editing Excel files, and delivering insightful video tutorials for YouTube channels. His work and learning interests vary from Microsoft Office Suites and Excel to Data Analysis, VBA, and Video recording and... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo