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?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Yes:

Code:
Sub Macro1()
    Dim rng As Range
    Set rng = Range("A1")
    If rng.Value = "x" Then
    ActiveSheet.Pictures.Insert( _
        "C:\Documents and Settings\...\untitled2.gif").Select
    End If
End Sub

Other more elegant ways of doing it, but this should get you started.

[Edited to take out single quotes which prevented line code from executing :rolleyes: ]
 
Upvote 0
If for this part, I already have the location of the file in cell b1, I guess as a string, how do I get it to open up the file located in b1?


Code:
'    ActiveSheet.Pictures.Insert( _ 
'        "C:\Documents and Settings\...\untitled2.gif").Select

and how do I get it to put the image in cell A2, centered and fit to the cell?
 
Upvote 0
Perhaps the easiest way is for you to record a macro. Open the file you want (that will give you the path, which you can later adjust), then move the picture as needed (locked to a cell, alignment, etc.). Then the recorded code will give you the essence of what must be done.
 
Upvote 0
I have this code so far, and it doesn't insert the picture

Code:
    Dim picnme As String
    Dim rng As Range
    Set rng = Range("S1")
    If rng.Value = "1" Then
    picnme = Range("P28")
'    ActiveSheet.Pictures.Insert( _
'        "C:\Documents and Settings\Pictures\"& picnme &").Select
    Selection.ShapeRange.IncrementLeft 162#
    Selection.ShapeRange.IncrementTop 0.75
    End If
End Sub

It runs it, but nothing happens. picnme, is the name of the picture, that I have located in cell P28.[/code]
 
Upvote 0
That was my fault. I put the single quote mark and it shouldn't be there (it prevents the code from executing on that line):

Code:
 Dim picnme As String 
    Dim rng As Range 
    Set rng = Range("S1") 
    If rng.Value = "1" Then 
    picnme = Range("P28") 
    ActiveSheet.Pictures.Insert( _ 
        "C:\Documents and Settings\Pictures\"& picnme &").Select 
    Selection.ShapeRange.IncrementLeft 162# 
    Selection.ShapeRange.IncrementTop 0.75 
    End If 
End Sub

Try it now.
 
Upvote 0
It gives me a syntax error on the ActiveSheet line now.

Expected List separator or ).
 
Upvote 0
Take out the middle "

Code:
    ActiveSheet.Pictures.Insert( _ 
        "C:\Documents and Settings\Pictures\& picnme &").Select
 
Upvote 0
Ok, I hope this is the last error and I'll be good to go, but the error now says:

Unable to get the insert property of the Picture Class

How do I do that?
 
Upvote 0
My guess is that you need to specify the file that you are inserting. Part in bold shows the area. Try to select a specific picture file with the extension on it.

Rich (BB code):
   ActiveSheet.Pictures.Insert( _ 
        "C:\Documents and Settings\Pictures\& picnme &").Select
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,142
Members
448,551
Latest member
Sienna de Souza

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