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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

shades

Well-known Member
Joined
Mar 20, 2002
Messages
1,550
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: ]
 

gottimd

Well-known Member
Joined
Jul 29, 2002
Messages
501
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?
 

shades

Well-known Member
Joined
Mar 20, 2002
Messages
1,550
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.
 

gottimd

Well-known Member
Joined
Jul 29, 2002
Messages
501

ADVERTISEMENT

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]
 

shades

Well-known Member
Joined
Mar 20, 2002
Messages
1,550
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.
 

gottimd

Well-known Member
Joined
Jul 29, 2002
Messages
501

ADVERTISEMENT

It gives me a syntax error on the ActiveSheet line now.

Expected List separator or ).
 

shades

Well-known Member
Joined
Mar 20, 2002
Messages
1,550
Take out the middle "

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

gottimd

Well-known Member
Joined
Jul 29, 2002
Messages
501
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?
 

shades

Well-known Member
Joined
Mar 20, 2002
Messages
1,550
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
 

Forum statistics

Threads
1,148,525
Messages
5,747,206
Members
424,068
Latest member
Salim khamis

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
Top