Option Explicit
Sub SetArrowsOnEmbeddedChartHiLowPoints()
'This code will examine Series 1 and place the up and down arrows at
' the highest and lowest points on the graph (if more than one then the leftmost one)
Dim lX As Long
Dim sngXHi As Single, sngXLo As Single
Dim sngYHi As Single, sngYLo As Single
Dim sngHiValue As Single, sngLoValue As Single
Dim lHiPos As Long
Dim lLoPos As Long
Dim sngXPos As Single, sngYPos As Single
Dim sngShapeYOffset As Single, sngShapeXOffset As Single
Dim shp As Excel.Shape
Dim sFormula As Variant
Dim sValues As Variant
sngYHi = 0
sngYLo = 100000
sFormula = Split(ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).Formula, ",")
sValues = Range(sFormula(2))
For lX = 1 To ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).Points.Count
ActiveSheet.ChartObjects(1).Select
sngXPos = ExecuteExcel4Macro("get.chart.item(1,1, ""S1P" & lX & """)")
sngYPos = ExecuteExcel4Macro("get.chart.item(2,1, ""S1P" & lX & """)")
If sngYPos > sngYHi Then
sngXHi = sngXPos
sngYHi = sngYPos
sngHiValue = sValues(lX, 1)
End If
If sngYLo > sngYPos Then
sngXLo = sngXPos
sngYLo = sngYPos
sngLoValue = sValues(lX, 1)
End If
Next
With ActiveSheet.ChartObjects(1).Chart
For lX = .Shapes.Count To 1 Step -1
If Left(.Shapes(lX).Name, 5) = "xyzzy" Then .Shapes(lX).Delete
Next
End With
Const lRedLine As Long = 3553420
Const lRedFill As Long = 5066944
Const lGrnLine As Long = 4163953
Const lGrnFill As Long = 5880731
Set shp = ActiveSheet.Shapes.AddShape(msoShapeUpArrow, 20, 20, 20, 20) 'X,Y,W,H
shp.Fill.ForeColor.RGB = lGrnFill 'RGB(255, 0, 0)
shp.Line.ForeColor.RGB = lGrnLine 'RGB(255, 0, 0)
shp.Name = "xyzzy_UpArrow"
Set shp = ActiveSheet.Shapes.AddShape(msoShapeDownArrow, 20, 40, 20, 20) 'X,Y,W,H
shp.Fill.ForeColor.RGB = lRedFill 'RGB(255, 0, 0)
shp.Line.ForeColor.RGB = lRedLine 'RGB(255, 0, 0)
shp.Name = "xyzzy_DnArrow"
sngShapeYOffset = ActiveSheet.Shapes("xyzzy_UpArrow").Width / 2
sngShapeXOffset = ActiveSheet.Shapes("xyzzy_UpArrow").Height / 2
With ActiveSheet.ChartObjects(1).Chart
ActiveSheet.Shapes("xyzzy_UpArrow").Copy
.Paste
With .Shapes(.Shapes.Count)
.Left = sngXHi - sngShapeXOffset
.Top = ActiveChart.ChartArea.Height - sngYHi - sngShapeYOffset
.Name = "xyzzy_Up"
End With
Set shp = .Shapes.AddShape(msoShapeRectangle, 20, 20, 20, 20) 'X,Y,W,H
With shp
.Left = sngXHi + sngShapeXOffset
.Top = ActiveChart.ChartArea.Height - sngYHi - sngShapeYOffset
.Name = "xyzzy_UpText"
With .TextFrame.Characters
.Text = sngHiValue
.Font.Color = 1
End With
.Fill.Visible = False
.Line.Visible = False
.TextFrame.AutoSize = True
End With
ActiveSheet.Shapes("xyzzy_DnArrow").Copy
.Paste
With .Shapes(.Shapes.Count)
.Left = sngXLo - sngShapeXOffset
.Top = ActiveChart.ChartArea.Height - sngYLo - sngShapeYOffset
.Name = "xyzzy_Dn"
End With
Set shp = .Shapes.AddShape(msoShapeRectangle, 20, 20, 20, 20) 'X,Y,W,H
With shp
.Left = sngXLo + sngShapeXOffset
.Top = ActiveChart.ChartArea.Height - sngYLo - sngShapeYOffset
.Name = "xyzzy_UDnText"
With .TextFrame.Characters
.Text = sngLoValue
.Font.Color = 1
End With
.Fill.Visible = False
.Line.Visible = False
.TextFrame.AutoSize = True
End With
End With
ActiveSheet.Shapes("xyzzy_UpArrow").Delete
ActiveSheet.Shapes("xyzzy_DnArrow").Delete
ActiveSheet.Range("A1").Select
Set shp = Nothing
End Sub