Inserting Photos into Excel

garrett1483

New Member
Joined
Sep 5, 2011
Messages
36
Hello All - First time using a forum but I thought this may be the only way to get the answer I need. I have an excel sheet and in Column A, I've got item#'s e.g. (A1007.jpg, A1008.jpg etc...). I have the corresponding photos in a folder on my desktop. I'd like a macro that will auto populate all the photos into the excel sheet in Column A, so the item#'s and the photos match up. Is there a way to do this? Any/All help would be appreciated.

Thanks
Garrett
 
The error says

Run-time error '1004':
Method 'Range' of object '_Worksheet' failed

The code I used is below:

Public Sub Test()
Dim sPath As String
Dim oCell As Range
Dim oRange As Range
Dim oPicture As Shape
Dim oSheet As Worksheet
sPath = "C:\Documents and Settings\gtsuyuki\Desktop\Crazy Close Out Photos\" 'Change to suit
Set oSheet = ActiveSheet
Set oRange = oSheet.Range("A1:A104" & oSheet.Range("A" & Rows.Count).End(xlUp).Row)
For Each oCell In oRange
If Dir(sPath & oCell.Text) <> "" And oCell.Value <> "" Then
Set oPicture = oSheet.Shapes.AddPicture(Filename:=sPath & oCell, LinktoFile:=msoFalse, savewithdocument:=msoTrue, Left:=oCell.Left, Top:=oCell.Top, Width:=1, Height:=1)
oPicture.ScaleHeight 1, True
oPicture.ScaleWidth 1, True
oCell.RowHeight = oPicture.Height
oCell.ColumnWidth = oPicture.Width / 4
Else
oCell.Offset(0, 1).Value = "Image file not found"
End If
Next oCell
End Sub

Highlighted in red is the error -

thanks
garrett
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Have you givin the code I posted a shot?

change your path and first cell in column A:A and you shoud be ready to test.

Jeff
 
Upvote 0
You've messed up the following line:

Code:
Set oRange = oSheet.Range("A1:A104" & oSheet.Range("A" & Rows.Count).End(xlUp).Row)

It should be either hard coded like this:

Code:
Set oRange = oSheet.Range("A1:A104")

It was originally finding the last used row in column A and looked like this:

Code:
Set oRange = oSheet.Range("A1:A" & oSheet.Range("A" & Rows.Count).End(xlUp).Row)

You have it trying to do both at the same time. Try setting that straight first.

Gary
 
Upvote 0
OMG!!! You guys are great - Thanks so much for your help. I didn't try the other version as i'm a total Newb to macros but I got the solution down. Thanks again guys!!!
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,476
Members
452,915
Latest member
hannnahheileen

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