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,

first of all this seams to be great function, salute to author ;)

Anyway, I try to implement it and IK'm having a problem with combing ShowPicD and MATCH function?

if I put something like formula below it works:
=ShowPicD("D:\00 Stampa\00 BAZA\LQ artwork\"&AE28&".png")


But if i put something like this, it doesn't...
=ShowPicD("D:\00 Stampa\00 BAZA\LQ artwork\"&INDEX(Baza!$AM$7:Baza!$AM$778;MATCH($E$5;Baza!$A$7:Baza!$A$778;0))&".png")

It gives false instead of picture...

Thx in advance,
Teo
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I've added the following two lines to the code below (as given in this thread) to keep the image proportions 100% constant as per the original image.

P.ScaleHeight Factor:=1, RelativeToOriginalSize:=msoTrue
P.ScaleWidth Factor:=1, RelativeToOriginalSize:=msoTrue

Code:
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 Pictures in Excel Formula
'and ShowPicD Function - Aspect Ratio
'and VB- Automatically Insert Picture

'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)
'P.ScaleHeight 1#, True, msoScaleFromTopLeft
'P.ScaleWidth 1#, True, msoScaleFromTopLeft
'P.SetShapesDefaultProperties
P.ScaleHeight Factor:=1, RelativeToOriginalSize:=msoTrue
P.ScaleWidth Factor:=1, RelativeToOriginalSize:=msoTrue

'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
Damon,

Your code worked great a couple of times, but then just stopped working. All it shows on the destination cells (where the picture should be) is FALSE. Not sure why this is happening, because last night the same cell was showing TRUE and the picture was coming, just like you had described.

To quickly describe what I am trying to do - I have an input sheet that allows me to input top 40 mobile app ranks on Play store every Monday. I am doing a vlookup to this input sheet, based on week chosen, to show a table on an output sheet. The table has 40 slots (3 cells / slot), with app name, rank and app logo in each slot. I have included your code to show the picture using the showPicD() function in the picture cell.

Could you help me with what could be wrong? I do not have sufficient working knowledge of VB to debug the code. Note - the only thing I have changed in your code is to resize the picture from (200, 200) to (40, 40).

Would really appreciate any help!

Arj


Hi Dan,

Yes, I guess it was just a matter of time before someone asked for that. Here's modified code that deletes the old picture each time it recalculates. Normally it will remember the old picture (the P variable), but when the file is first opened it has no memory of the previous picture so I added logic for this case to delete the first picture it finds with its upper-left corner in the cell. I named this function ShowPicD:

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 P Is Nothing Then
'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
Else
P.Delete
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


So, are you a HS teacher in Pennsylvania, Palo Alto, or ? (Either way, you are to be commended).

Damon
 
Upvote 0

Forum statistics

Threads
1,224,538
Messages
6,179,412
Members
452,912
Latest member
alicemil

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