How to insert photos from a folder into a cell in Excel automatically?

Asked

Viewed 1,517 times

0

I need to insert several images in Excel, however it is difficult to insert one by one, are more than 300 photos, and there will be updates of these photos.

I was wondering if there’s a way to perform such a procedure?

1 answer

1

Open Excel, and press ALT+F11, will open the VBE (Visual Basic Editor). As right click on "Vbaproject" and choose "Insert > Module":

inserir a descrição da imagem aqui

Enter the code below in the window (see comments):

Option Explicit

Sub Insert()

    Dim strFolder As String
    Dim strFileName As String
    Dim objPic As Picture
    Dim rngCell As Range

    strFolder = "D:\pasta_com_as_imagens\" 'altere o caminho para onde estão as imagens
    If Right(strFolder, 1) <> "\" Then
        strFolder = strFolder & "\"
    End If

    Set rngCell = Range("E1") 'célula de início

    strFileName = Dir(strFolder & "*.jpg", vbNormal) 'arquivos jpg

    Do While Len(strFileName) > 0
        Set objPic = ActiveSheet.Pictures.Insert(strFolder & strFileName)
        With objPic
            .Left = rngCell.Left
            .Top = rngCell.Top
            .Height = rngCell.RowHeight
            .Placement = xlMoveAndSize
        End With
        Set rngCell = rngCell.Offset(1, 0)
        strFileName = Dir
    Loop

End Sub

Press CTRL+B to save the code and choose the option in "Type" as shown in the figure below:

inserir a descrição da imagem aqui

Close the VBE window.

In Excel, press ALT+F8 and click "Run":

inserir a descrição da imagem aqui

This will insert all images from the folder indicated in the line-by-line code of the sheet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.