how to apply xlMarkerStyles to a Chart Plot in Excel vba without directly using the xlMarkerStyleAutomatic - vba

I have a x,y scatter chart with lines connecting the dots from one source. I have between 1 and 8 lines and I need to have the MarkerStyle assigned to each line. Since the lines are not fixed and depend on the current data, I can't say which lines are there.
In general I could just assign
ActiveChart.FullSeriesCollection(i).MarkerStyle = xlMarkerStyleAutomatic
But that also assigns unwanted/unreadable Markers. So could I create a Collection with the Markerstyles I want and than assign that?
I tested
Dim colMarker As Collection
Set colMarker = New Collection
colMarker.Add "xlMarkerStyleCircle"
colMarker.Add "xlMarkerStyleSquare"
colMarker.Add "xlMarkerStyleTriangle"
With ActiveChart.FullSeriesCollection(i)
.MarkerStyle = colMarker(1)
End With
But the error msg is wrong type
What type do I need?
Thanks Kaz

You can set the marker style for each series individually:
Dim s as Series
s = ActiveChart.SeriesCollection.NewSeries
s.MarkerStyle = xlMarkerStyleCircle
Edit:
To assign different marker styles use the approach you suggested, but when adding styles to the collection it should be done like this:
colMarker.Add xlMarkerStyleDiamond
etc, ie without quotation marks.

Related

Adding Names to Scatter Plot Points Without Modifying X-Values (Excel 2007 VBA)

I am attempting to modify the tooltips of a scatter plot using Visual Basic code so that the name of the data points are in the tooltips when you click on individual data points, along with the x and y data from the plot.
I have already attempted to use:
ThisWorkbook.Worksheets("Scatter Plot").ChartObjects(1).Chart.SetSourceData Source:=ThisWorkbook.Worksheets("Sheet1").Range(col1 & ", " & col2), PlotBy:=xlColumns
ThisWorkbook.Worksheets("Scatter Plot").ChartObjects(1).Chart.SeriesCollection(1).XValues = ThisWorkbook.Worksheets("Sheet1").Range(col3)
But when I try to use it, the macro overwrites the X-value data already stored in the data points in order to add the names to the scatter plot tooltips. Is there any way that I can modify the names of the scatter plot points without modifying their x-values or using Add-Ins?
The following code sets the Data Label (Data Callout) to the values provided in the Range "Sheet1!$K$10:$K$20". Change this reference to the cells where you have the content you would like to display and you should be good to go.
Best regards
Christian
ThisWorkbook.Worksheets("Scatter Plot").ChartObjects(1).Chart.SeriesCollection(1).DataLabels.Format.TextFrame2.TextRange. InsertChartField msoChartFieldRange, "=Sheet1!$K$10:$K$20", 0
Then add the following to also display them properly:
With ThisWorkbook.Worksheets("Scatter Plot").ChartObjects(1).Chart.SeriesCollection(1).
.ShowRange = True
.ShowCategoryName = False
.ShowValue = False
End With

SeriesCollection array size in PPT VBA

I am trying to add data labels to all my series in a bar chart but the amount of series varies from chart to chart because this is a function used for many charts. So, I used a For Loop to add them, but I need to determine the size of the SeriesCollection array. Would there be a function that would do so? When I try to use .Size it gives me an error.
Below is what I tried:
With Chart
For i = 1 To .SeriesCollection.Size
.SeriesCollection(i).Points(i).HasDataLabel = True
Next i
End With
Some of the stuff in the Chart object model isn't properly hooked up to Intellisense. You'll notice that you get no suggestions when you type .SeriesCollection.
.SeriesCollection.Count will give you what you're after, I think.

Excel graph legend doesn't match markers

I've formatted the markers of a chart using VBA:
Dim sc As SeriesCollection
Set sc = MySheet.ChartObjects("MyChart").Chart.SeriesCollection
Dim p As Point, s As Series
For Each s In sc
For Each p In s.Points
p.Format.Fill.ForeColor.RGB = s.Format.Line.ForeColor.RGB
p.Format.Fill.BackColor.RGB = s.Format.Line.ForeColor.RGB
p.MarkerSize = 3
Next p
Next s
However, the markers in the chart area don't match up with the markers in the legend (the border colour is the same, but the fill colour in the legend is different). Note the colour inside the boxes on the right (the legend):
I've looked around at a few different properties of the Chart family, but can't find what controls this. The Point properties MarkerBackgroundColor and MarkerForegroundColor seemed likely, but don't fix it. How can I fix this?
Since you were looping the seriescollection, you could simply do all the markers in one go:
For Each s In sc
s.MarkerBackgroundColor = s.Border.Color
s.MarkerSize = 3
Next s
Found with a bit more digging:
Worksheet.ChartObjects(Index).Chart.Item(Index).Format.Fill.ForeColor.RGB
Pretty lengthy!

Changing Colors for a clustered bar graph based on axis label

Well I am trying to change the colors for my graph based on label conditions.
First of all, this is how the graph looks like.
I was trying to change the values based on three conditions. Warning, Critical and Emergency. Basically having three different colors for each of the conditions. For example all Emergencies be red, all critical be orange and all warning be yellow. "Title" is x-axis in my case and "criticality" happens to be the cluster.
I tried following the thread
excel vba changing bar chart color for a data point based on point value
I also looked at this thread Changing Bar colors using VBA based on category label
I havent had any luck trying to use the code from both the threads.
This code from the second thread actually made sense for me to use in my case.
Dim c As Chart
Dim s As Series
Dim iPoint As Long
Dim nPoint As Long
Set c = ActiveChart
Set s = c.SeriesCollection(1)
nPoint = s.Points.Count
For iPoint = 1 To nPoint
If s.XValues(iPoint) = "avg" Then
s.Points(iPoint).Interior.Color = RGB(255, 0, 0)
End If
Next iPoint
But not sure what variables should I be changing. Any help is welcomed!
Thanks.
the s.XValues(iPoint) will contain Title and Criticality concatenated.
Try to change:
If s.XValues(iPoint) = "avg"
to:
If right(s.XValues(iPoint), 9) = "Emergency"
In this way you will check if the last 9 letters of the label are exactly "Emergency".
You should be able to modify your code to add another condition for Critical or Warning.
Hope this help,
The Macro Guru

Change the color for each line

I got a console application that draws line in a 3D CAD program. Now to make this more clear i want to change these lines in different colors.
My code read variables from a text-file and calculates data from it and then makes a lines from this calculated data.
This process gets repeated with every line in the text file wich contains data.
Now i want visual basic to change the color everytinme a new line is drawn, so i get different colored lines.
I tried using a For.. To.. Step method, but this didnt work. I also tried to use the variables from my text file( these are different so when a new line got read the RGB code will change) but this will geve me only a lot of blue colors.
Any suggestions?
EDit:
This is what i use to draw the curves, the RGB code has to cahnge everytime when a line with new data is made:
' Creating a Curve2d object by using the above defined points
objLineString = objLineStrings.AddByPoints(PointCount:=points, points:=dataArray)
objGeometricStyle = objLineString.Style
color = objGeometricStyle.LinearColor
objGeometricStyle.LinearColor = RGB(0,0,0)
What about :
Dim rand As New Random() ' Used to generate random numbers
Dim colors(100) as Integer
' Create the colors
For i as Integer = 0 to 100 Step 1
colors(i) = RGB(rand.Next(256), rand.Next(256), rand.Next(256))
Next
For i As Integer = 0 To 100 Step 1 ' Adjust to your needs
' Creating a Curve2d object by using the above defined points
objLineString = objLineStrings.AddByPoints(PointCount:=points, points:=dataArray)
objGeometricStyle = objLineString.Style
color = objGeometricStyle.LinearColor
objGeometricStyle.LinearColor = colors(i Mod 100) ' Mod returns the remainder of i / 100, so it's always less than 100.
Next
This won't always give you "pretty" colors, but they'll be different for each line. If you want control over the generated colors, you could set up an array of predefined colors and use these in your iteration.