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

0

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

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

    
asked by anonymous 20.03.2018 / 22:50

1 answer

1

Open Excel, and press ALT + F11 , will open VBE (Visual Basic Editor). Right-click on "VBAProject" and choose "Insert> Module":

Enterthecodebelowintothewindow(seecomments):

OptionExplicitSubInsert()DimstrFolderAsStringDimstrFileNameAsStringDimobjPicAsPictureDimrngCellAsRangestrFolder="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 under "Type" as shown in the figure below:

ClosetheVBEwindow.

InExcel,pressALT+F8andclick"Run":

This will insert all the images of the folder indicated in the line by line code of the worksheet.

    
20.03.2018 / 23:26