Link to home
Start Free TrialLog in
Avatar of flemingg62
flemingg62

asked on

Import Photos (jpg) in to excel where the file name in stored in Columns A

Import Photos (jpg) in column X in excel where the file name and Path are stored in Columns A (About 100 rows)
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Use this macro. Change the column number in line 8 if your data column is not actually column 'X'.

Sub GetPictures()
Dim lngLastRow As Long
Dim lngRow As Long
Dim fNameAndPath As Variant
Dim img As Picture
Const COL = 24 ' = column X

With ActiveSheet
    lngLastRow = .UsedRange.Rows.Count
    For lngRow = 1 To lngLastRow
        If LCase(Right$(.Cells(lngRow, COL), 3)) = "jpg" Then
            fNameAndPath = .Cells(lngRow, COL)
            If Dir(fNameAndPath) = "" Then
                MsgBox "Picture name in row " & lngRow & " not found"
                Exit Sub
            End If
            
            Set img = .Pictures.Insert(fNameAndPath)
            With img
               .Left = ActiveSheet.Cells(lngRow, COL + 1).Left
               .Top = ActiveSheet.Cells(lngRow, COL + 1).Top
               .Width = ActiveSheet.Cells(lngRow, COL + 1).Width
               .Height = ActiveSheet.Cells(lngRow, COL + 1).Height
               .Placement = 1
               .PrintObject = True
            End With
        End If
    Next
End With
End Sub

Open in new window

importing 100s of images into excel file looks a bad idea...
may easily corrupt data...
especially images are mid-large size, say, 250KB min
then excel file will be min 25MB!

is there any reason to do that?
excel file will be min 25MB
I'm not sure that is true. As a test using the above code I added 100 copies of a 1285 KB picture to a workbook and the workbook size only increased by 5 KB.
I'm not sure that is true. As a test using the above code I added 100 copies of a 1285 KB picture to a workbook and the workbook size only increased by 5 KB

this cannot be real :)

if you insert 100 copies, then probably excel only inserts 1 and 100 links...
+ your image is probably empty, and when zipped it becomes so tiny...
if it is real image, then it will be huge!

here, I inserted 5 images...
moderate sizes...

size is 2.5 MB!

100 such image will make excel file 50MB!

29205293.xlsx
Here is the workbook I created. You'll see that the image is sized to fit in a cell. The code is slightly modified for the purposes of the test.
29205293.xlsm
if we rename excel file as zip and open with winrar/winzip
we will see the images are stored inside

User generated imagefor my sample, here 5 images...
some of them are png (no compression, some jpg, small but cannot be compressed more, it is already compressed)

so, you can just look at the size of 100 images and can estimate what will be the size of excel at the end...

Here is the workbook I created. You'll see that the image is sized to fit in a cell. The code is slightly modified for the purposes of the test

I cannot see any images!
it means images are not in excel, just shortcut/link to actual files
and can be seen only on your machine and as long as those files are not moved/deleted/renamed...

this is your excel file, no media/images inside
User generated image
You are correct that you can't see any images but if you change the code so that it refers to one of your images then you will see them. I'm going to wait until the Askers responds before I comment again.
Avatar of flemingg62
flemingg62

ASKER

Perfect Thanks But instead of ending the sub for missing photos can it skip to the next line
I having a go myself at it but not very good with vba
If you delete line 17 it will do that and if you also delete line 16 you won't see the message.
Wait, that's not correct. Be back in a few minutes.
SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect Thanks
the code is mainly from Martin Liss's post...
that should be selected as assisted at least...
Thanks for clarifying
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography" section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Distinguished Expert in Excel 2018
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2020
              Experts Exchange Top Expert VBA 2018 to 2020