# How to insert image (.jpg, .png) from folder based on name file in cell to specific sheet with VBA.



## mesharp (Dec 15, 2022)

I have problem about VBA, I searched an internet and no one can achieved my problem using VBA.

images are several type such as .png , .jpg and different size.
I want using VBA to only one click the button and import the image from my folder to excel by multiple specific name and multiple worksheets.
and resize the image to be I specified.

Example
The folder link which contains images is = cell A1


PictureFile nameDestination sheet1001.pngsheet12002.jpgsheet23003.pngsheet3


----------



## John_w (Dec 16, 2022)

Welcome to MrExcel forums.



mesharp said:


> The folder link which contains images is = cell A1


In the macro below, the folder path to the images is in cell D1, with the cell values shown in your post in columns A:C on a sheet named "Images".



mesharp said:


> resize the image to be I specified.


The width and height of all images is specified in this part of the code:

```
Const pictureWidth = 200
    Const pictureHeight = 300
```



mesharp said:


> I want using VBA to only one click the button


Assign this macro to the button.


```
Public Sub Insert_Images_To_Sheets()

    Dim folder As String, imageFile As String
    Dim lastRow As Long, r As Long
    Dim pic As Shape
    
    Const pictureWidth = 200
    Const pictureHeight = 300
    
    With Worksheets("Images")
        folder = .Range("D1").Value
        If Right(folder, 1) <> "\" Then folder = folder & "\"
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For r = 2 To lastRow
            imageFile = .Cells(r, "B").Value
            With Worksheets(.Cells(r, "C").Value).Range("A1")
                Set pic = .Worksheet.Shapes.AddPicture(Filename:=folder & imageFile, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, _
                                                       Left:=.Left, Top:=.Top, Width:=pictureWidth, Height:=pictureHeight)
            End With
        Next
    End With
    
End Sub
```


----------

