Arrow To Indicate Special Points Using VBA - vba

I have seen some examples which uses an Arrow to Indicate Special Points on Excel chart like this. But i want to achieve this using VBA. For example if some point on chart is greater then 90 then it shows an arrow corresponding to that point.
Please suggest on how should I go about it in VBA. Any help would be appreciated.
Update
Apart from just changing the point color is there any other good suggestion to make that point more prominent. Update 2
Right now i am using this code.
For Each oCell In Range("e4:e" & LastRow) 'loop
If oCell.Value < sd13 Then 'rule 13s
Range("V4").Value = "Rule 13s voilated!"
Range("V4:w4").Interior.Color = RGB(255, 0, 0)
ActiveWorkbook.Sheets("LDL-C").ChartObjects("Chart 1047").Chart.SeriesCollection(1).Points(j).MarkerBackgroundColor = RGB(255, 0, 0)
End If
Next

Apart from just changing the point color is there any other good suggestion to make that point more prominent.
Would this help?
With ActiveChart
For i = 1 To .SeriesCollection.Count
a = .SeriesCollection(i).Values
For l = 1 To .SeriesCollection(i).Points.Count
If mymax < a(l) Then
mymax = a(l)
.SeriesCollection(i).DataLabels.Select
Selection.Format.Line.Visible = msoTrue
Selection.Format.Line.Visible = msoFalse
.SeriesCollection(i).Points(l).DataLabel.Select
.SeriesCollection(i).Points(l).Select
.SeriesCollection(i).DataLabels.Select
.SeriesCollection(i).Points(l).DataLabel.Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.Weight = 2
End With
End If
Next l
Next
End With
SNAPSHOT
Another snapshot

Not sure how to do it with an arrow but here is a way to just change the colour of the point of interest:
With ActiveSheet.ChartObjects(ChartName).Chart.SeriesCollection("NCDs")
For currentPoint = 1 To .Points.Count
If Range("G" & currentPoint + 34).Value = True Then
With .Points(currentPoint).Format
.Fill.BackColor.RGB = RGB(50, 150, 50)
.Fill.ForeColor.RGB = RGB(50, 150, 50)
.Line.ForeColor.RGB = RGB(50, 150, 50)
End With
Else
With .Points(currentPoint).Format
.Fill.BackColor.RGB = RGB(150, 50, 50)
.Fill.ForeColor.RGB = RGB(150, 50, 50)
.Line.ForeColor.RGB = RGB(150, 50, 50)
End With
End If
Next currentPoint
End With
Just change the names and the condition clause...
Also maybe the .Points(currentPoint) object has x,y location properties which you could use to position your arrow. Not sure about that though but it seems like a good starting point.

Yeah, gotta have VBA. Problem with VBA is that someone has to remember to run the procedure, or set up a Worksheet_Calculate event, or whatever, so when the data changes, which it inevitably does, the chart keeps up with the data.
So here's my non-VBA approach, which relies on Excel formulas.
Simple data, supplied by Siddharth in his answer. I've added a column, which I call MAX. The formula in cell C2, copied down to C11, is
=IF(B2=MAX(B$2:B$11),B2,NA())
The first chart plots the regular series of data in a clustered column chart. The second chart has MAX added to it. In the third chart I've changed the Overlap to 100%, so the blue bar covers the corresponding gray bar. Next chart I've added data labels to the MAX series. In the last chart I've formatted the data label to show series name, and the font color =matches the bar color.
So here is the original data and chart (upper) and changed data with changed chart (below). The formulas did it all, no need to somehow rerun the VBA.
What's cool is if I have a tie for first place, I get two labeled blue bars, with no extra effort.
Not a big stretch to add a third series to indicate the MIN.

ActiveChart.FullSeriesCollection(1).Select
With Selection
.MarkerStyle = 8
.MarkerSize = 5
End With
Selection.MarkerStyle = 2
ActiveChart.ChartArea.Select
With Selection.Format.Line

Related

Why does changing Chart LINE properties in VBA do not work?

I copy-pasted an embedded chart into a new worksheet. I was able to change the source data but could not change the line weight and the color. I don't understand what is happening... great to get feedback! Here is my code:
With .SeriesCollection(1)
.Values = DP.Sheets("Sheet1").Range("A18:A58")
.XValues = DP.Sheets("Sheet1").Range("C18:C58")
.Format.Line.Weight = 1
.Format.Line.ForeColor.RGB = RGB(0, 0, 0)
End With
SeriesCollection(1) has existing links which is replaced by those references/ranges. This changes the line color to 'AUTOMATIC' and excel assigns a very odd color which I want to change back to black. Unsure with what is happening. Help please!

Any codes that does clear color completely for powerpoint shape?

'I have tried to apply a color by vba with this code to a shape
.Fill.ForeCorlor.RGB = RGB(250, 0, 0)
'but soon I realize this color stay in memory* even though I tried to clear it with
.Fill.Transparency = 0.5 'Or
.Fill.Visible = msoFalse
'I do not want to use .Fill.ForeColor.TGB = RBB(255, 255, 255)
to set another color as it may overlay and block other content
*In memory: I mean when I applied a color to a shape and try to clear it with the above method, The next time I target shape with RGB(230,0,0), this shape that I thought has been previously set the color and clear still get targeted.
Appreciate for any help on this
G

Chart Series Line: Bring to Front, Send to Back

If I use VBA in Excel to make a line chart with multiple series, and two of the series' data are very similar so that their chart series lines partly overlap, the last one written is in front of earlier ones written.
In the Worksheet_Change event, I want to be able to go back and forth between which chart series line is in front, based on user actions that change the data. Can I do that without deleting and recreating the chart?
Here's how I'm identifiying the series line, for example here's series 2:
Sheet1.ChartObjects("MyChart").Chart.SeriesCollection(2)
Getting TypeName on that returns Series. I see Series in help, but with no information on its properties and methods. I don't see Series in the Object Browser (I'm on Excel 2007). I was able to get a list of properties and methods in the context help dropdown, but I didn't see anything promising in the dropdown.
So, can I bring a chart series to the front/send it to the back, without deleting and recreating the chart?
Here is a simple non-VBA way to highlight one series in a chart.
Below I show X data in column B, Y data for series alpha, beta, and gamma in columns C:E, and extra data in column F.
I have a chart, and below the chart a combo box inserted using Developer tab > Insert > Form Controls > Combo Box. I formatted the control (right click) and set the Input Range to K16:18, and the Cell Link to J16.
I selected F2:F16, with F2 being the active cell. I entered =INDEX(C2:E2,$J$16) in the formula bar, then held Ctrl while I pressed the Enter key. This filled the formula into the entire range. Essentially the formula takes the value from the three cells to the left, based on the Cell Link of the combo box. Since I've selected "beta" in the combo, J16 contains the value 2, and column F shows the values of the second data column.
The chart was made using the entire data range. The first three series were formatted with light gray lines, and the fourth (duplicate) series was formatted with a color that stands out in comparison. All data is visible at once, but only one is highlighted.
What you can do is change all the series line colors to a light color except for one. That one you color dark or bright, ie Red. Then you can repeat the process (assign the macro to a button on/near the chart) to highlight each series in turn.
Sub CycleSeriesColors()
Dim seriesCount As Integer, i As Integer, smod
Static s '"Remember" what series we are on between calls.
ActiveSheet.ChartObjects("Chart 1").Activate
' ActiveChart.PlotArea.Select
seriesCount = ActiveChart.SeriesCollection.Count
s = s + 1
smod = s Mod (seriesCount + 1)
' ActiveChart.ChartArea.Select
If Not smod = 0 Then
With ActiveChart.SeriesCollection(smod).Format.Line
.Visible = msoTrue
.Visible = msoTrue
.ForeColor.RGB = RGB(192, 0, 0)
.Transparency = 0
End With
For i = 1 To seriesCount
If Not i = smod Then 'the series is to backgrounded
With ActiveChart.SeriesCollection(i).Format.Line
.Visible = msoTrue
.Visible = msoTrue
.ForeColor.RGB = RGB(240, 240, 240)
.Transparency = 0.85
End With
End If
Next i
Else
Randomize
For i = 1 To seriesCount
With ActiveChart.SeriesCollection(i).Format.Line
.Visible = msoTrue
.Visible = msoTrue
.ForeColor.RGB = RGB(((i * i) ^ 0.6 * 3), ((i * i) ^ 0.6 * 8), ((i * i) ^ 0.6 * 12))
.Transparency = 0
End With
Next i
End If
End Sub

VBA chart/object border/fill colour change

Hi I am currently working in Microsoft Excel 2010 and am trying to change the colour (and other properties) of a specific data point on a chart. So far I have tried:
ActiveChart.SeriesCollection(1).Points(4).Select
With Selection
.Interior.ColorIndex = 3
.MarkerSize = 10
.MarkerStyle = 8
End With
The code runs fine and the rest of the macro runs perfectly, (with size changing to 10 and style changing) apart from the colour staying the default blue! I have tried changing the colour index to Black and White - this doesn't seem to fix the issue.
After poking around I also tried:
ActiveChart.SeriesCollection(1).Points(4).Select
With Selection
.Interior.Color = RGB(255, 0, 0)
.MarkerSize = 10
.MarkerStyle = 8
End With
I also tried:
.ForeColor = RGB(255, 0, 0)
and
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
But I'm unsure how the last one works fully.
I'd really appreciate some help with this and I'd be happy to PM an example copy if someone would be willing to take a look!
(To reiterate - the activechart part of the function works fine - as does the following marker size and style changes)
-- Hash
You are trying to do this using properties that belong to cells
Try using this instead
With ActiveChart.SeriesCollection(1).Points(4)
.MarkerBackgroundColor = RGB(255, 0, 0)
.MarkerSize = 10
.MarkerStyle = 8
end with

VBA: set gradient fill background angle

I have the following problem: I want to set a chart background with "Gradient Fill" and change the angle with a macro.
Recording a Macro does not help. The only thing I got is the following:
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 117, 117)
.BackColor.RGB = RGB(0, 203, 92)
.TwoColorGradient msoGradientDiagonalUp, 1
End With
In reality I want to set the colors in advance and use the macro just for orientation. The only thing the macro should do is:
If variable = A then 'select the chart and set angle to 45%
If variable = B then 'select the chart and set the angle to 135%
and so on.
I do not want to set manually the colors because I will have 4 colors and it useless to set them every time since their relative position is always the same within the gradient pattern.
Any Idea?
Ok, I made it.
It is very simple, but you cannot easily find this info online.
You need to activate your chart and after:
ActiveChart.PlotArea.Format.Fill.GradientAngle = 45