Insert JPG into cell based on cell value

WayneTN

Board Regular
Joined
May 5, 2002
Messages
75
Hello. I'm very new with coding and couldn't find what I need after multiple searches. I think the solution will be simple for someone who knows what they are doing.

I'm working on a spreadsheet to track the team scoring during the Ryder Cup golf matches.
I have 3 pictures of 3 different flags (USA, Europe, Halved) in a folder called C:\MyFiles\Flags\
The flags' files are named USA.jpg, Europe.jpg, Halved.jpg

During the golf matches, various cells on my spreadsheet's Scoreboard worksheet (within the range of C9:M17) will be populated from other worksheets with words saying either "USA", "EUROPE", or "HALVED" (whoever wins that match).

What I want to do is this. When a cell on my Scoreboard worksheet says "USA", I want my spreadsheet to pop into that cell the picture of the USA flag, and so on. The pictures are already sized for each cell, so there should be no problems there.

Any help is greatly appreciated.
Thanks in advance,
WayneTN
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Or

Check and change references where required.
In the "Scoreboard" sheet module
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C9:M17")) Is Nothing Then
    Call Get_Flags
End If
End Sub
In a regular module.
Code:
Sub Get_Flags()
Dim shp As Shape, rng As Range, c As Range, flagArr, i As Long
Set rng = Sheets("Scoreboard").Range("C9:M17")
flagArr = Array("USA", "Europe", "Halved")

    For Each shp In Sheets("Scoreboard").Shapes
        If Not Intersect(shp.TopLeftCell, rng) Is Nothing Then shp.Delete
    Next shp
    
    For Each c In rng
        For i = LBound(flagArr) To UBound(flagArr)
            If LCase(c.Value) = LCase(flagArr(i)) Then
                ActiveSheet.Pictures.Insert("C:\MyFiles\Flags\" & flagArr(i) & ".jpg").Select

                    With Selection.ShapeRange
                        .LockAspectRatio = msoFalse
                        .Left = c.Left
                        .Top = c.Top
                        .Width = c.Width
                        .Height = c.Height
                    End With
            End If
        Next i
    Next c

End Sub
 
Upvote 0
Change these 8 lines
Code:
ActiveSheet.Pictures.Insert("C:\MyFiles\Flags\" & flagArr(i) & ".jpg").Select
With Selection.ShapeRange
.LockAspectRatio = msoFalse
.Left = c.Left
.Top = c.Top
.Width = c.Width
.Height = c.Height
End With
to this single line
Code:
ActiveSheet.Shapes.AddPicture "C:\MyFiles\Flags\" & flagArr(i) & ".jpg", msoFalse, msoTrue, c.Left, c.Top, c.Width, c.Height
 
Upvote 0
Can I delete the temp workbook created to test the code supplied to you?
 
Upvote 0
Hi Wayne,

Both Excelisfun and Leila G have done videos on this solution

Excel Picture Lookup: 5 easy steps for dynamic images
Thanks for your valuable input. I watched the Leila G video where she explains how to insert a JPG into a cell based on programming (dynamic images), and I adapted her work to accomplish what I needed. She chose not to use VBA code but to only use Excel programming (indirect function, name management) to cause national flag pics to pop into cells. It was a little tricky making everything look neat and involved cropping and sizing the flags and cells as Leila explains in the video, but I made everything work. Now I have a Ryder Cup golf template with 28 cells ready to display the flag of the country that wins each of the 28 matches. Thanks everyone who supported my efforts!
 
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