VBA - Insert Multiple Images into a sheet

Tarkin

New Member
Joined
Feb 12, 2019
Messages
12
Hi

I have searched everywhere but somehow the VBA codes I found on the web to do this routine have not worked.

This is what I am trying to do:

I have a set of images (PNG format) located in a folder (C:\MyImages)

I want a VBA code to automatically load these into a workbook (in a sheet called 'Screenshots') and insert them in cells of Column B. These cells have been resized to accommodate an ideal resolution size of the images.

Ideally, I would like the file name of each images to be inserted in Column A cells, next to the relevant image.

Any help would be greatly appreciated! :)

Thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try with this code

Code:
Sub Insert_Multiple_Images()
'
    Dim wPath As String, i As Long, wFile As Variant
    Dim sh As Worksheet
    
    Application.ScreenUpdating = False
    Set sh = Sheets("Screenshots")
    sh.DrawingObjects.Delete
    sh.Cells.ClearContents
    
    wPath = "C:\MyImages\"
    If Right(wPath, 1) <> "\" Then wPath = wPath & "\"
    i = 2
    wFile = Dir(wPath & "*.png")
    
    Do While wFile <> ""
        With sh.Pictures.Insert(wPath & wFile)
            .Placement = xlMoveAndSize
            .ShapeRange.LockAspectRatio = msoFalse
            .Top = sh.Cells(i, "B").Top + 1
            .Left = sh.Cells(i, "B").Left + 1
            .Width = sh.Cells(i, "B").Width - 2
            .Height = sh.Cells(i, "B").Height - 2
            sh.Cells(i, "A").Value = wFile
            i = i + 1
        End With
        wFile = Dir()
    Loop
    
    Application.ScreenUpdating = True
    MsgBox "End"
End Sub
 
Upvote 0
Not that good at VBA but please let me know if this will do:
Code:
Dim ImgName As String
Dim Path
Path = "C:\MyImages"
ImgName = Dir("C:\MyImages\*.png")
rowNum = 1
Do While ImgName <> ""

With Screenshots.Pictures.Insert(Path & ImgName)
    With .ShapeRange
        .LockAspectRatio = msoTrue
        .Width = 75
        .Height = 90 'Row height is specified in points, image height is also specified in points
                     'If default size of excel rows is 15 points, the image height will be 90 points = 6 rows
    End With
.Left = Range("B" & rowNum).Left
.Top = Range("B" & rowNum).Top
.Placement = 1
.PrintObject = True
End With
Range("A" & rowNum).Value = ImgName

ImgName = Dir
rowNum = rowNum + 7
Loop
 
Last edited:
Upvote 0
DanteAmor,your code was flawless and I achieved the result I wanted, many thanks!


Try with this code

Code:
Sub Insert_Multiple_Images()
'
    Dim wPath As String, i As Long, wFile As Variant
    Dim sh As Worksheet
    
    Application.ScreenUpdating = False
    Set sh = Sheets("Screenshots")
    sh.DrawingObjects.Delete
    sh.Cells.ClearContents
    
    wPath = "C:\MyImages\"
    If Right(wPath, 1) <> "\" Then wPath = wPath & "\"
    i = 2
    wFile = Dir(wPath & "*.png")
    
    Do While wFile <> ""
        With sh.Pictures.Insert(wPath & wFile)
            .Placement = xlMoveAndSize
            .ShapeRange.LockAspectRatio = msoFalse
            .Top = sh.Cells(i, "B").Top + 1
            .Left = sh.Cells(i, "B").Left + 1
            .Width = sh.Cells(i, "B").Width - 2
            .Height = sh.Cells(i, "B").Height - 2
            sh.Cells(i, "A").Value = wFile
            i = i + 1
        End With
        wFile = Dir()
    Loop
    
    Application.ScreenUpdating = True
    MsgBox "End"
End Sub
 
Upvote 0
Exceladd1ct, I triedyour code but I get an error at

'WithScreenshots.Pictures.insert(Path & ImgName)'

>Am I missingsomething ?


Not that good at VBA but please let me know if this will do:
Code:
Dim ImgName As String
Dim Path
Path = "C:\MyImages"
ImgName = Dir("C:\MyImages\*.png")
rowNum = 1
Do While ImgName <> ""

With Screenshots.Pictures.Insert(Path & ImgName)
    With .ShapeRange
        .LockAspectRatio = msoTrue
        .Width = 75
        .Height = 90 'Row height is specified in points, image height is also specified in points
                     'If default size of excel rows is 15 points, the image height will be 90 points = 6 rows
    End With
.Left = Range("B" & rowNum).Left
.Top = Range("B" & rowNum).Top
.Placement = 1
.PrintObject = True
End With
Range("A" & rowNum).Value = ImgName

ImgName = Dir
rowNum = rowNum + 7
Loop
 
Upvote 0
Sorry for big delay, My mistake:
Code:
Dim ImgName As String
Dim Path
Path = "C:\MyImages"
ImgName = Dir("C:\MyImages\*.png")
rowNum = 1
Do While ImgName <> ""

With Sheets("Screenshoots").Pictures.Insert(Path & ImgName)
    With .ShapeRange
        .LockAspectRatio = msoTrue
        .Width = 75
        .Height = 90 'Row height is specified in points, image height is also specified in points
                     'If default size of excel rows is 15 points, the image height will be 90 points = 6 rows
    End With
.Left = Range("B" & rowNum).Left
.Top = Range("B" & rowNum).Top
.Placement = 1
.PrintObject = True
End With
Range("A" & rowNum).Value = ImgName

ImgName = Dir
rowNum = rowNum + 7
Loop
 
Upvote 0
The code stops at

With Sheets("Screenshots").Pictures.insert(Path & ImgName)



Sorry for big delay, My mistake:
Code:
Dim ImgName As String
Dim Path
Path = "C:\MyImages"
ImgName = Dir("C:\MyImages\*.png")
rowNum = 1
Do While ImgName <> ""

With Sheets("Screenshoots").Pictures.Insert(Path & ImgName)
    With .ShapeRange
        .LockAspectRatio = msoTrue
        .Width = 75
        .Height = 90 'Row height is specified in points, image height is also specified in points
                     'If default size of excel rows is 15 points, the image height will be 90 points = 6 rows
    End With
.Left = Range("B" & rowNum).Left
.Top = Range("B" & rowNum).Top
.Placement = 1
.PrintObject = True
End With
Range("A" & rowNum).Value = ImgName

ImgName = Dir
rowNum = rowNum + 7
Loop
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top