use reference cell to insert a picture in a specific cell

sully3868

New Member
Joined
Mar 10, 2022
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I have some VBA code here but I keep getting an error on line 11. I need the code to look at the text in the B column and then go to the specified folder and find the jpg file of the same name and then insert it into the excel sheet. This is a very simplified excel sheet, I need to change the code after for a different excel sheet which is much larger but the code should still work. Thanks.


Private Sub CommandButton1_Click()
Dim animal_pic As Pictures
Dim pic_location As String
Dim animal_name As String
For i = 2 To 6
animal_name = Worksheets("Sheet1").Cells(2, 2).Value
pic_location = "C:\Users\osullij2\Desktop\Animal Picture\" & Worksheets("Sheet1").Cells(2, 2).Value & ".jpg"

With Worksheets("Sheet1").Cells(i, 3)
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 = 170
animal_pic.ShapeRange.Height = 100
End With
Next
Worksheets("Sheet1").Cells(1, 1).Select
End Sub
 
I'd love to say that fixed it but unfortunately it did not. Still only the tiger is showing + the error
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Have you checked for typos & extra spaces?
 
Upvote 0
Yes. I had a typo on the file name but I corrected that. Everything else seems to be good
 
Upvote 0
In that case I am not sure why you get the error as it works ok for me.
 
Upvote 0
you just changed the file locations and the names in the excel sheet and it worked fine for you?
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,509
Members
449,089
Latest member
RandomExceller01

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