Pictures in Excel Formula

conradg

New Member
Joined
Sep 3, 2004
Messages
16
Hello

I am trying something new. I would like to find out how to put a picture into a formula so that when: example if A1 equals a certain number then a picture will pop up on the worksheet.

So in otherwords - a picture will automaticly pop up - so I need a formula to include the picture.

If you need further explanation please let me know

Thank you
Gregg Conrad
 
Hi,

I want to show pictures in a cell in excel based on a certain style number. let's say I have column A which includes a list of style numbers. I want column B to display the picture of each style number by looking at the value of the style number and inserting the picture which has the same name from a location on my pc. I need it fit in the cell and move the one below so it doesn't overlap the picture below.

I currently have this code

Function ShowPicD(PicFile As String, Width As Integer, Height As Integer) 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, 50, 50)
P.ScaleHeight 1, True
P.ScaleWidth 1, True
P.Height = 50
P.Width = 50
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

This is the formula I have in excel: =ShowPicD(LEFT(CELL("filename",A1),FIND("[",CELL("filename",A1))-1)&"pics\"&A1&".jpg", 50, 50)

The pictures are reinserted each time I refresh the formula or copy it. There's also not enough space between the rows for it to show properly, so they're being overlaped by each other.

Is there a formula or code that will fix my problem?

Thanks a lot
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Unfortunately I cannot help with the question above but I do have another question regarding this thread.

The "ShowpicD" function has worked very well for me but now I need to be able to share this workbook. When the workbook is shared, whatever image was last displayed remains on the screen and no new images appear when the referenced cell is changed. Why won't the macro run normally when the workbook is shared?
 
Upvote 0
Hi all,

This thread has been a great help to me already so thank you all!

There is one thing I was hoping someone would be able to help me add to it, I've messed around a bit trying to get it to work but I'm not very good on Excel.

When I use ShowPicD I would like it to change the row height to equal that of the picture.

I thought after doing some reading that I could use something like:
Code:
Rows(3).RowHeight = Range(J3)

I used Rows(3) because the image is always in the Row 3, but if it could automatically adjust whatever row the picture is in then that would be even better. Range(J3) refers to a cell that contains the vertical resolution of the image.
 
Upvote 0
I've been trying to work out how to this for a long time, so thank you all for posting your replies. I've copied all the answers and they work great. the one thing that I'd like to be able to do is have the pictures change depending on an address in another cell.

if cell a2 = 11402 112 ave then display one picture and have the picture change depending on the address.

thanks for any help

one thing the =ShowPicD(LEFT(CELL("FileName",A2),FIND("[",CELL("FileName",A2))-1)&"pictures\"&B2&"002.jpg") will only work if I copy and paste into another cell. Otherwise it will show a true value
 
Upvote 0
I get the "True" and see the picture attempt to show but then disappears. Anyone know what i'm doing wrong?
 
Upvote 0
I got the pic to show up but I have other problems the pic is showing up on all the other worksheets in the workbook?
 
Upvote 0
To avoid having the picture show up on other sheets, you need to avoid inserting into the active sheet. Here's a modified version of the function, which also allows you to optionally shift the image horizontally or vertically (plus a few other improvements I've incorporated which I can't take credit for). :)

FYI, I used this as part of a scoring spreadsheet I created for my Cub Scout's Pinewood Derby. I took pictures of all the cars and was able to display them during each heat as the races progressed - worked awesome! This really made the spreadsheet - the boys and families loved it.

Thanks especially to the original author, Damon Ostrander, and others out there who have used and improved the function (especially PA HS Teacher).

~ Steve

Function ShowPicD(PicFile As String, _
Optional iWidth As Single = 118, _
Optional iHeight As Single = 89, _
Optional ShiftHorizontal As Single = 0, _
Optional ShiftVertical As Single = 0) As String
'Shows a picture and deletes previous picture when picfile changes.

'Adapted from ShowPicD code posted by Damon Ostrander, with special
'thanks to PA HS Teacher for some nice improvements.
'See http://www.mrexcel.com/forum/showthread.php?t=100737
'and http://www.mrexcel.com/forum/showthread.php?t=145913
'and http://www.mrexcel.com/forum/showthread.php?t=145024

'Major modifications are as follows:
'1) Use correct variable types (Single) for width, etc.
'2) Width and height are optional (credit to PA HS Teacher).
'3) Enable user to just send in width or height, calculate other value
' assuming a 4x3 image.
'4) Add option to shift the image horizontally and vertically.
'5) Link to picture instead of embedding in the file - makes saves
' faster and reduces file size (credit to PA HS Teacher).
'6) Fix issue with picture showing up on other worksheets (credit to
' PA HS Teacher).
'7) Name the picture based on cell location so that we do not have to
' search for pictures by location.
'8) Return a String instead of a Boolean in order to display "No Image"
' in cell if no image exists.
'9) Probably a few other tweaks. :)

On Error GoTo Done

' Default to 4x3 image size if one of dimensions is specified as 0
' Note - if we wanted to generally scale the picture that can be
' done (instead of doing this) - see note on this further in the
' routine. We assume that the user will send in one of these values
' (not both as zero).
If iHeight = 0 Then
iHeight = 0.75 * iWidth
ElseIf iWidth = 0 Then
iWidth = 4 / 3 * iWidth
End If

'Look for a picture already over cell (note we name the picture based
'on cell location below so we can use the name as our search criteria)
Dim AC As Range
Set AC = Application.Caller
Dim SheetName As String
SheetName = AC.Parent.name
'Note - this will delete max of one existing picture over cell
Dim P As Shape
For Each P In Worksheets(SheetName).Shapes
If P.Type = msoLinkedPicture Then
If P.name = SheetName & "-" & CStr(AC.row) & ":" & CStr(AC.column) Then
P.Delete
Exit For
End If
End If
Next P

'Special case if picture file is blank - return empty string and exit
If PicFile = "" Then
ShowPicD = ""
Exit Function
End If

'Notes on AddPicture method:
'AC.Left = the position (in points) of the upper-left corner of the picture
'relative to the upper-left corner of the document.
'AC.Top = the position (in points) of the upper-left corner of the picture
'relative to the top of the document (thus we subtract ShiftVertical below
'instead of add it).
'The 3rd argument is set to false to save a link to the file, versus embedding
'in the Excel file - makes our files much smaller and saves much faster.
Set P = Worksheets(SheetName).Shapes.AddPicture(PicFile, msoCTrue, msoFalse, _
AC.left, AC.top, iWidth, iHeight)

'Shift the picture if needed. Note: Excel may not shift the picture
'exactly as we request - it can come out 0.2 off from where we want
'it. The original ShowPicD function searched for the previous picture
'based on location, which would fail due to this slight discrepancy.
'Thus we now name the picture based on cell location instead.
P.IncrementLeft ShiftHorizontal
P.IncrementTop -ShiftVertical

'Uncomment the code below to see the possible 0.2 shift mentioned above
'Dim err_left As Single, err_top As Single
'err_left = P.left - left
'err_top = P.top - top
'Dim result As Integer
'result = MsgBox("Image shift - " & PicFile & vbCrLf & _
' "Left = " & Str(err_left) & vbCrLf & "Top = " & Str(err_top))

'Name P based on calling cell location
'Note - I use CStr instead of Str as Str adds a space before the number
P.name = SheetName & "-" & CStr(AC.row) & ":" & CStr(AC.column)

'Note - another way to scale the image is to do the below (use this
'if you cannot live with the 4x3 image scaling earlier in the function),
'but this will be slightly slower as the picture will be scaled twice.
'P.ScaleHeight 1, msoCTrue
'P.ScaleWidth 1, True
'P.Height = iHeight
'Also can set P.Width instead
ShowPicD = ""
Exit Function
Done:
ShowPicD = "No Image"
End Function
 
Upvote 0
This works perfectly, but I have a question; is there any way adding a function to make the picture show up on bottom?

I want to have a picture show up as a background picture behind a text box,
but when I use this macro it ends up on top.
 
Upvote 0
Add this line (for example after the "P.name = " line):

' Send picture to the back
P.ZOrder msoSendToBack
 
Upvote 0

Forum statistics

Threads
1,215,262
Messages
6,123,938
Members
449,134
Latest member
NickWBA

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