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
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try something like this:
replace:

With Worksheets("Sheet1").Cells(i, 3)
Set animal_pic = ActiveSheet.Pictures.Insert(pic_location)

animal_pic.Top = .Top
animal_pic.Left = .Left

with

Set animal_pic = ActiveSheet.Pictures.Insert(pic_location)
With animal_pic
.Top = Worksheets("Sheet1").Cells(i, 3).Top
.Left = Worksheets("Sheet1").Cells(i, 3).Left


..etc
 
Upvote 0
Thanks for the reply. Code still not working unfortunately :(
 

Attachments

  • jpg.png
    jpg.png
    30.1 KB · Views: 10
Upvote 0
Hi & welcome to MrExcel.
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 6
      animal_name = Worksheets("Sheet1").Cells(i, 2).Value
      pic_location = "C:\Users\osullij2\Desktop\Animal Picture\" & animal_name & ".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
 
Upvote 0
still the same error, not sure what to try next
 

Attachments

  • Screenshot 2022-03-10 155936.png
    Screenshot 2022-03-10 155936.png
    26.2 KB · Views: 11
Upvote 0
What is the error you get?
 
Upvote 0
"Run-time error '1004'
Unable to get the Insert property of the Pictures class"
 
Upvote 0
In that case check the the path & file name are correct. It's probably a typo somewhere or extra spaces.
Also do the values in B2:B6 have the file extension?
 
Upvote 0
yes they do. Its kind of half working now. The error is still coming up but when I click end rather than debug, the picture of the tiger appears in cell C2.
 
Upvote 0
yes they do
Than that is the problem. You are adding the extension on again in the code.
Use
VBA Code:
pic_location = "C:\Users\osullij2\Desktop\Animal Picture\" & animal_name
 
Upvote 0

Forum statistics

Threads
1,214,798
Messages
6,121,636
Members
449,043
Latest member
farhansadik

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