Code to attach pictures to an excel sheet

sully3868

New Member
Joined
Mar 10, 2022
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello, I have this code below to add pictures to an excel file. The only issue with it is that when its looking down through the list and matching the picture name from the excel sheet to the folder, if there is no match the code stops. Is there an easy way to make it so that if there is no match, the code will jump over that picture and go to the next?





Private Sub CommandButton1_Click()

Dim animal_pic As Picture

Dim pic_location As String

Dim animal_name As String

For i = 2 To 329

animal_name = Worksheets("1").Cells(i, 1).Value

pic_location = "W:\thumbnails\" & Worksheets("1").Cells(i, 1).Value & ".jpg"



With Worksheets("1").Cells(i, 16)

Set animal_pic = ActiveSheet.Pictures.Insert(pic_location)



animal_pic.Top = .Top

animal_pic.Left = .Left

animal_pic.ShapeRange.LockAspectRatio = msoFalse

animal_pic.Placement = xlMoveAndSize

animal_pic.ShapeRange.Width = 120

animal_pic.ShapeRange.Height = 90

End With

Next

Worksheets("1").Cells(1, 1).Select

End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
How about
VBA Code:
Private Sub CommandButton1_Click()

Dim animal_pic As Picture

Dim pic_location As String

Dim animal_name As String

For i = 2 To 329

animal_name = Worksheets("1").Cells(i, 1).Value

pic_location = "W:\thumbnails\" & Worksheets("1").Cells(i, 1).Value & ".jpg"

If Dir(pic_location) <> "" Then

With Worksheets("1").Cells(i, 16)

Set animal_pic = ActiveSheet.Pictures.Insert(pic_location)



animal_pic.Top = .Top

animal_pic.Left = .Left

animal_pic.ShapeRange.LockAspectRatio = msoFalse

animal_pic.Placement = xlMoveAndSize

animal_pic.ShapeRange.Width = 120

animal_pic.ShapeRange.Height = 90

End With
End If
Next

Worksheets("1").Cells(1, 1).Select

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,055
Messages
6,122,902
Members
449,097
Latest member
dbomb1414

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