Insert .jpg based on Cell value in Excel?

SinnaminGrrl

Board Regular
Joined
Feb 21, 2006
Messages
65
Hi,

I have some code that changes the colour of a cell based on the value of that cell, not sure how useful this is as I could just simply use conditional formatting, but I found this code here as a starting point to what I was trying to accomplish.

I have a pivot table that is constantly changing values, the column is a result of a number of days...not really relevant.... however If the cell contains a value of greater then 7 I want to insert a .JPG of a lil stop sign, if less then 7 then a little green go sign will be inserted, can someone help? This is what I have so far..just changes cell color.

Sub DoOnSelection()
Dim oCell As Range
For Each oCell In Selection
If oCell.Value > 7 Then
oCell.Interior.ColorIndex = 3
End If

Next
End Sub
 

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)
This basic code will update one cell with a new photo. You will need to change the code to work with each of the cells in question.


Sub AddPictureToCell()
'Standard module code, like: Module1.
Dim ws As Worksheet
Dim myCell As Range
Dim myPhotos As ShapeRange

Application.ScreenUpdating = False

Set ws = Worksheets("Sheet1")
Set myCell = ws.Range("B2")

On Error GoTo myNone
Set myPhotos = myCell.Pictures.ShapeRange
myPhotos.Delete

myNone:
myCell.ColumnWidth = 7
myCell.RowHeight = 30

ws.Pictures.Insert("U:\Excel\Test\aPhoto2.jpg").Select

With Selection
.Top = myCell.Top
.Left = myCell.Left
.Width = myCell.Width
.Height = myCell.Height
.Placement = xlMoveAndSize
.PrintObject = True
End With

myCell.Select
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Joe this is a great start!!

Now I just need to get the picuture to insert based on the value of a cell... any ideas on how to do that?
 
Upvote 0
Just change the Range that it is to work on and change the Picture Drive:/Path/FileName.Ext and you should be good to go!


Sub AddPictureToCell()
'Standard module code, like: Module1.
Dim cell As Object
Dim ws As Worksheet
Dim myRange As Range
Dim myPhotos As ShapeRange

Application.ScreenUpdating = False
Set ws = Worksheets("Sheet1")
Set myRng = ws.Range("B2:B12")

On Error GoTo noPics
Set myPhotos = ws.Pictures.ShapeRange

If myPhotos.Count > 0 Then myPhotos.Delete

noPics:
For Each cell In myRng
cell.ColumnWidth = 7
cell.RowHeight = 30

If cell.Offset(0, 1).Value > 7 Then ws.Pictures.Insert("U:\Excel\Test\aPhoto2.jpg").Select
If cell.Offset(0, 1).Value < 7 Then ws.Pictures.Insert("U:\Excel\Test\aPhoto.jpg").Select

With Selection
.Top = cell.Top
.Left = cell.Left
.Width = cell.Width
.Height = cell.Height
.Placement = xlMoveAndSize
.PrintObject = True
End With

Next cell
Application.ScreenUpdating = True

ActiveSheet.Range("B1").Select
End Sub
 
Upvote 0
FWIW, if you're not married to the .JPG's you're using, you might be able to avoid macros all together by using =IF(A1<=7,CHAR(152),CHAR(196)), setting the font to "Wingdings 2", and using conditional formatting to change the font color to green/red according to whether A1 is <= or > 7.

That could give you a red octagon or green circle...not 100% what you wanted, but might be close enough.
 
Upvote 0
Actually, using Matt's idea, you can just format the font to wingdings 2 and use a custom format right on the cells with the value (or a simple =A1 type of formula to transfer the value into the cell.) The custom format would be:<ul>[*][Green][<=7]"˜";[Red][>7]"Ä";General[/list]
 
Upvote 0
You could also attach the macro code version to that sheets Change Event and it will be dynamic and automatically change as the values change, rather than you needing to run the macro.

The macro may be more envolved, but it does look better than formatting though!
 
Upvote 0

Forum statistics

Threads
1,223,979
Messages
6,175,757
Members
452,667
Latest member
vanessavalentino83

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