Inserting multiple (specific) images from a folder

dafydd_jwc

New Member
Joined
Dec 1, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet of all the stock our company has, and would like to add images to it (we produce board games & jigsaws!). We have product codes - a letter and 3 or 4 digits (e.g. A123)

All the images will be named as their product code - e.g. "A123.jpg"

I have a column which has all the codes, I have a column which has concatenated the product code and '.jpg', but I don't know how to write the code to insert the image associated with that product in the next cell over. The images would need to go into column D, starting at cell D2.

Hope someone is able to help!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
1638349867589.png

This sort of idea.
 
Upvote 0
There is a issue about size on cell --> size of picture. If you have standard size of cells, picture will be really small.
Run InsertPictureFromCells().

VBA Code:
Option Explicit
Function InsPict(ByVal PicFilename As String, ByVal Target As Range) As String
Dim p As IPictureDisp, t As Double, L As Double
Dim w As Double, h As Double
Dim InsPic As Shape
    If TypeName(ActiveSheet) <> "Worksheet" Then Exit Function
    On Error Resume Next
    Target.Parent.Shapes("R" & Target.Row & "C" & Target.Column).Delete
    On Error GoTo 0
    If Dir(PicFilename) = "" Then Exit Function
    Set p = LoadPicture(PicFilename)
    w = Target.Width - 2
    h = w * p.Height / p.Width
    If h > Target.Height - 2 Then
        h = Target.Height
        w = h * p.Width / p.Height
    End If
    With Target
        Set InsPic = .Parent.Shapes.AddPicture(PicFilename, msoFalse, msoTrue, .Left, .Top, w, h)
    End With
    Set p = Nothing
    If Not InsPic Is Nothing Then
        InsPic.Name = "R" & Target.Row & "C" & Target.Column
        InsPic.LockAspectRatio = msoTrue
        InsPic.Left = Target.Left + (Target.Width - InsPic.Width) / 2
        InsPic.Top = Target.Top + (Target.Height - InsPic.Height) / 2
    End If
    InsPict = ""
End Function

Sub InsertPictureFromCells()
Dim cell As Range
With ActiveSheet
    For Each cell In .Range("C2:C" & .Cells(Rows.Count, "C").End(xlUp).Row)
        cell.Offset(0, 1).Value = InsPict(cell.Value, cell.Offset(0, 1))
    Next cell
End With
End Sub
 
Upvote 0
With declared pictures size (then cell is automatically resize to fit it in).

Excel Formula:
InsPict(cell_with_JPG path, cell where picture have to be, sizeW, sizeH)

VBA Code:
Option Explicit
Function InsPict(ByVal PicFilename As String, ByVal Target As Range, maxW As Integer, maxH As Integer) As String
Dim p As IPictureDisp, t As Double, L As Double
Dim w As Double, h As Double
Dim InsPic As Shape
    If TypeName(ActiveSheet) <> "Worksheet" Then Exit Function
    On Error Resume Next
    Target.Parent.Shapes("R" & Target.Row & "C" & Target.Column).Delete
    On Error GoTo 0
    If Dir(PicFilename) = "" Then Exit Function
    Set p = LoadPicture(PicFilename)
    w = maxW
    h = w * p.Height / p.Width
    If h > maxH Then
        h = Target.Height
        w = h * p.Width / p.Height
    End If
    With Target
        Set InsPic = .Parent.Shapes.AddPicture(PicFilename, msoFalse, msoTrue, .Left, .Top, w, h)
    End With
    Set p = Nothing
    If Not InsPic Is Nothing Then
        InsPic.Name = "R" & Target.Row & "C" & Target.Column
        InsPic.LockAspectRatio = msoFalse
        Rows(Target.Row).RowHeight = InsPic.Height
        Columns(Target.Column).ColumnWidth = InsPic.Width * (54.29 / 288)
        InsPic.Left = Target.Left + (Target.Width - InsPic.Width) / 2
        InsPic.Top = Target.Top + (Target.Height - InsPic.Height) / 2
    End If
    InsPict = ""
End Function

Sub InsertPictureFromCells()
Dim cell As Range
With ActiveSheet
    For Each cell In .Range("C2:C" & .Cells(Rows.Count, "C").End(xlUp).Row)
        cell.Offset(0, 1).Value = InsPict(cell.Value, cell.Offset(0, 1), 100, 100)
    Next cell
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,012
Messages
6,122,682
Members
449,091
Latest member
peppernaut

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