Excel: Display an Image and IF Condition
JB_A
Posted messages
307
Status
Membre
-
cjard1 -
cjard1 -
Hello,
I have a formula like: IF(F40=1,"Yes",IF(F40=2,"No",IF(F40=0,"")))
And instead of displaying Yes or No, I would like to display an image based on the value of F40.
Do you have any idea of the syntax/formula to use?
Thank you in advance for your reply
I have a formula like: IF(F40=1,"Yes",IF(F40=2,"No",IF(F40=0,"")))
And instead of displaying Yes or No, I would like to display an image based on the value of F40.
Do you have any idea of the syntax/formula to use?
Thank you in advance for your reply
Configuration: Excel 2000
3 réponses
Hello J_BA, Raymond
This formula should work:
the 2nd criterion is optional, if the images are in the same directory as the workbook
But first: (I'm cheating Raymond, don’t worry, the image display function doesn't exist in Excel)
you go to VBA: Right-click on the tab name / View Code
you switch windows and arrive in VBA
Insert Menu / Module
and you copy and paste the lines below
The function adjusts the size of the image to the size of the cell, and if they are merged cells, it takes that into account
Best regards
Wilfried
P.S.: don’t forget to set your thread as resolved when you find what you are looking for. Thank you
This formula should work:
=If(A1="YES",Image("Myfile.jpg","MyPath"),image("")) the 2nd criterion is optional, if the images are in the same directory as the workbook
But first: (I'm cheating Raymond, don’t worry, the image display function doesn't exist in Excel)
you go to VBA: Right-click on the tab name / View Code
you switch windows and arrive in VBA
Insert Menu / Module
and you copy and paste the lines below
Function Image(img_name As Variant, Optional path As String = "") As String ' Declaration of variables Dim ref As Range, sh As Shape, flag As Boolean ' ref: the cell that triggers the function ' sh: the shapes ' flag: a flag defining if the shape is found Application.Volatile ' defines a function that recalculates automatically ' test the type of variable either a cell or an alphanumeric value Select Case TypeName(img_name) Case "Range" ' it's a cell reference Image = img_name.Value Case "String" ' it's an alphanumeric value Image = img_name Case Else ' it's an error Image = "#ERROR" Exit Function End Select ' the path is an optional parameter, if omitted, the value is the workbook path If path = "" Then path = ThisWorkbook.Path ' the path doesn't necessarily end with \ so I add it If Right(path, 1) <> "\" Then path = path & "\" Set ref = Application.Caller ' assign ref to the cell that calls the function If ref.MergeCells = True Then Set ref = Range(ref.MergeArea.Address) flag = False ' flag initialization For Each sh In ref.Worksheet.Shapes ' I loop through all shapes ' I test its name constructed below to know if it's the right shape If "Img-" & ref.Address = Left(sh.Name, Len(ref.Address) + 4) Then flag = True: Exit For Next If flag = True Then ' it's the right shape ' I now check if it's the same as the one in the formula to avoid reprocessing ' The time saving is not negligible If "Img-" & ref.Address & "-" & Image = sh.Name Then GoTo end ' perfect equality, I exit End If On Error Resume Next ' error control, if the shape does not exist yet, the next instruction causes an error sh.Delete ' I delete the shape If Image = "" Then Exit Function ' the value is "" so no shape to assign ' I insert the shape, with the image in it adjusting the necessary sizes for the cell Set sh = ref.Worksheet.Shapes.AddPicture(path & Image, True, True, ref.Left, ref.Top, ref.Width, ref.Height) sh.Name = "Img-" & ref.Address & "-" & Image ' I define its name to find it later end: Image = "Img" & ref.Address ' I assign a name for the result End Function
The function adjusts the size of the image to the size of the cell, and if they are merged cells, it takes that into account
Best regards
Wilfried
P.S.: don’t forget to set your thread as resolved when you find what you are looking for. Thank you
Bravo and thank you again.
Great tip, it works well for me, however, once the image appears it doesn't go away if the cell value no longer matches. Is there a way to remove it if the cell value changes?