Code amendment needed for inserting images into a cell (VBA)

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Code:
Sub Insert_Multiple_Images()

Set Image_Names = Range("B3:B8")
Image_Location = ThisWorkbook.Path & "\Images”
Image_Format = ".jpg”

Set Cell_Reference = Range("C3:C8")

For i = 1 To Image_Names.Rows.Count
    For j = 1 To Image_Names.Columns.Count
        Set Image = ActiveSheet.Pictures.Insert(Image_Location + "\" + Image_Names.Cells(i, j) + Image_Format)
        Image.Top = Cell_Reference.Cells(i, j).Top
        Image.Left = Cell_Reference.Cells(i, j).Left
        Image.ShapeRange.Height = 45
        Image.ShapeRange.Width = 20
    Next j
Next i

End Sub

On this line:
Code:
Image_Format = ".jpg”

I want it to be able to accept all image formats or extensions.

Then on these lines:
Code:
Image.Top = Cell_Reference.Cells(i, j).Top
Image.Left = Cell_Reference.Cells(i, j).Left

I want to get the images centered in the cells.

My row height is 30 at the moment.

Thanks in advance
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Code:
Sub CenterImages()
    Dim shp As Shape
    For Each shp In ActiveSheet.Shapes
           With shp
               .Top = .TopLeftCell.Top + (.TopLeftCell.Height - .Height) / 2
               .Left = .TopLeftCell.Left + (.TopLeftCell.Width - .Width) / 2
           End With
    Next
End Sub

I found this code from stack overflow which is doing the center alignment for me.

Which means I am left with the dynamic file extension for the images

I tried

“.*”

But it didn’t work
 
Upvote 0
Code:
Set Image = ActiveSheet.Pictures.Insert(Image_Location & "\" & Image_Names.Cells(i, j) & Image_Format)
HTH. Dave
 
Upvote 0
Code:
Set Image = ActiveSheet.Pictures.Insert(Image_Location & "\" & Image_Names.Cells(i, j) & Image_Format)
HTH. Dave

With the above line, when I modified the image_format as :

Code:
Image_Format = ".*”

I get run time error 1004
Unable to get the Insert property of the picture class
 
Upvote 0
@NdNoviceHlp
The above didn’t work either.
I don’t know why this line

Code:
Image_Format = ".*”

Is throwing that error message.
I have used that to load images into an image control on a userform before.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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