View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.misc
Ken Johnson Ken Johnson is offline
external usenet poster
 
Posts: 1,073
Default How do I reference graphic images in formulas rather than text

Hi Daigles,

I think I understand your problem now.
It's in the heading of your post!
You're using a formula to reference the desired image and the change in
the formula's value (resulting from a sheet calculation) is not
detected by Excel as a Worksheet_Change event so nothing happens.
If that is the case then you need to use the Worksheet_Calculate event
instead.

The following code acts on a table consisting of the range $B$2:$D$11
as before.
The five pie images (I just used grouped autoshapes to make mine) are
present on the same sheet (they can be part of a legend) and they have
been given the names "None", "Quarter", "Half", ThreeQuarters" and
"Full" (as before).
Everytime the Worksheet is calculated all the old pie images in the
table are deleted then replaced with a new image depending on the value
in the cell.
Blank cells, cells with negative values and cells with a value greater
than 1 receive no image.
Cells with a value greater than or equal to 0 and less than 0.125
receive the "None" pie
Cells with a value greater than or equal to 0.125 and less than 0.375
receive the "Quarter" pie
Cells with a value greater than or equal to 0.375 and less than 0.625
receive the "Half" pie
Cells with a value greater than or equal to 0.625 and less than 0.875
receive the "ThreeQuarter" pie
Cells with a value greater than or equal to 0.875 and less than or
equal to 1 receive the "Full" pie.
I've made it easy for you to control the positioning of the image in
the cell. At the top of the code there are two string constants,
strHorizontal and strVertical. Edit their values to suit you needs. You
do not have to worry about the case used when setting these values, but
make sure the spelling is correct (I have used the American English
spelling for "Center" even though my version of the English language
(Australian English) spells it "Centre").

Again, if you have any problems adapting the code to your needs let me
know.

Private Sub Worksheet_Calculate()
Application.ScreenUpdating = False
'Edit the value of strHorizontal depending
'on where you want the Pie image to be
'horizontally positioned in the cell.
'Values used by the code are...
'"Left", "Center" or "Right" (not case sensitive)
Const strHorizontal As String = "right"
'Edit the value of strVertical depending
'on where you want the Pie image to be
'Vertically positioned in the cell.
'Values used by the code are...
'"Top", "Center" or "Bottom" (not case sensitive)
Const strVertical As String = "bottom"
Dim iHorizontal As Single
Dim iVertical As Single
Select Case UCase(strHorizontal)
Case "LEFT"
iHorizontal = 0
Case "CENTER"
iHorizontal = 0.5
Case "RIGHT"
iHorizontal = 1
End Select
Select Case UCase(strVertical)
Case "TOP"
iVertical = 0
Case "CENTER"
iVertical = 0.5
Case "BOTTOM"
iVertical = 1
End Select
Dim strActiveCellAddress As String
strActiveCellAddress = ActiveCell.Address
Dim rngCell As Range
Dim strPie As String
Dim ShpPie As Shape
For Each rngCell In Range("B2:D11")
On Error Resume Next
Me.Shapes("~" & rngCell.Address).Delete
strPie = ""
On Error GoTo 0
Select Case rngCell.Value
Case ""
Case Is < 0
Case Is < 0.125
strPie = "None"
Case Is < 0.375
strPie = "Quarter"
Case Is < 0.625
strPie = "Half"
Case Is < 0.875
strPie = "ThreeQuarters"
Case Is <= 1
strPie = "Full"
End Select
If strPie < "" Then
Me.Shapes(strPie).Copy
Range(rngCell.Address).PasteSpecial
Selection.Name = "~" & rngCell.Address
Set ShpPie = Me.Shapes("~" & rngCell.Address)
ShpPie.Left = rngCell.Left + _
iHorizontal * (rngCell.Width - ShpPie.Width)
ShpPie.Top = rngCell.Top + _
iVertical * (rngCell.Height - ShpPie.Height)
End If
Next rngCell
Range(strActiveCellAddress).Select
End Sub

Ken Johnson