Insert Pictures based on Cell Values?

gottimd

Well-known Member
Joined
Jul 29, 2002
Messages
501
Is it possible to do this? Lets say I have in Cell A1, the letter x.

Basically, something that would say IF cell A1=x, then insert picture C:\X.jpg into Cell A2. Is this something that can be done?
 
When I select the specific picture, then yes it works, but that defeats the whole purpose of the Macro. Is there a way to do this?

The picnme = Cell p28. In this Cell P28 is the pictures name, which will change once data is refreshed. So how do I get it to recognize the extension of the jpg. Right now for instance, I have in Cell p28, "Money01.jpg"

Any ideas?
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Here is a user defined function Damon Wrote for me a few months ago. I use it all the time and like to advertise it.
http://www.mrexcel.com/board2/viewtopic.php?t=104322&postdays=0&postorder=asc&start=10

It places a picture aligned with the corner of the cell from which the function is called.

For example,
=If(A1="X",PicD(B1),"") where B1 contains the path of the desired picture.


Code:
Function ShowPicD(PicFile As String) As Boolean 
'Same as ShowPic except deletes previous picture when picfile changes 
Dim AC As Range 
Static P As Shape 
On Error GoTo Done 
Set AC = Application.Caller 
If PicExists(P) Then 
P.Delete 
Else 
'look for a picture already over cell 
For Each P In ActiveSheet.Shapes 
If P.Type = msoLinkedPicture Then 
If P.Left >= AC.Left And P.Left < AC.Left + AC.Width Then 
If P.Top >= AC.Top And P.Top < AC.Top + AC.Height Then 
P.Delete 
Exit For 
End If 
End If 
End If 
Next P 
End If 
Set P = ActiveSheet.Shapes.AddPicture(PicFile, True, True, AC.Left, AC.Top, 200, 200) 
ShowPicD = True 
Exit Function 
Done: 
ShowPicD = False 
End Function 

Function PicExists(P As Shape) As Boolean 
'Return true if P references an existing shape 
Dim ShapeName As String 
On Error GoTo NoPic 
If P Is Nothing Then GoTo NoPic 
ShapeName = P.Name 
PicExists = True 
NoPic: 
PicExists = False 
End Function
 
Upvote 0
gottimd said:
When I select the specific picture, then yes it works, but that defeats the whole purpose of the Macro. Is there a way to do this?

The picnme = Cell p28. In this Cell P28 is the pictures name, which will change once data is refreshed. So how do I get it to recognize the extension of the jpg. Right now for instance, I have in Cell p28, "Money01.jpg"

Any ideas?

Is the picture name "Money01" or is it "Money01.jpg" (It should be the former one).

Try this line

Code:
    ActiveSheet.Pictures.Insert( _
        "C:\Documents and Settings\Pictures\" & picnme & ".jpg").Select

I tried your full code, now and it works fine.
 
Upvote 0
How are you getting it to work....

This is my exact code:
Code:
    Dim picnme As String
    Dim rng As Range
    Dim ArrImg
    Set rng = Range("S27")
    picnme = Range("P28")
    If rng.Value = 1 Then   
    Range("C27").Select
    ActiveSheet.Pictures.Insert( _
        "C:\Documents and Settings\Pictures\" & picnme & ".jpg").Select
    Selection.ShapeRange.IncrementLeft 162#
    Selection.ShapeRange.IncrementTop 0.75
    End If
End Sub

It still gives me that property thing error, and the value of the cell picnme is "Money01" not money01.jpg?

How is it working for you, what do I have wrong?
[/code]
 
Upvote 0
That's great. Glad you stayed with it. And thanks for coming back to report that.
 
Upvote 0

Forum statistics

Threads
1,216,473
Messages
6,130,838
Members
449,597
Latest member
buikhanhsang

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