Using VBA to create markers on a chart series

Still Active 1 Reputation point
2022-04-19T04:25:27.863+00:00

I was referred to this forum from MS Community

I want to use VBA to set and remove line markers (e.g. diamond, triangle)
on a series in an Excel chart. The Excel VBA Chart Object Model
is different for "chart sheets" and sheets with "embedded charts",
and I want my code to work with both. I created a "test workbook"
(that is separate from my "Project workbook"). In the test workbook
I created a chart sheet and a sheet with embedded charts.
After much fiddling around, I developed code that works for both.

So, to start at the beginning, I used Macro Record in the project workbook to generate
initial code. That recorded macro is shown below.

ActiveSheet.ChartObjects("Basic_Symbol_Chart").Activate
ActiveChart.FullSeriesCollection(1).Select
ActiveChart.FullSeriesCollection(1).Points(148).Select

Selection.MarkerStyle = 8
Selection.MarkerSize = 15
With Selection.Format.Fill
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorAccent1
    .ForeColor.TintAndShade = 0

'''' .ForeColor.Brightness = 0 ' This fails when run as a macro
.Solid
End With
Selection.Format.Line.Visible = msoFalse ' The intent here is to have
' solid fill in the marker
' and no line around the marker.
' It makes no difference if
' I comment this line out.

The above steps worked while I was recording. When I later tried to run
this macro (in the project worksheet), however, the statement
".ForeColor.Brightness = 0" generated an error! When I commented
that statement out, the macro runs but does absolutely nothing. Note that I
did put debug statements in the macro, so I know that it does run.

That is when I created my test workbook. After much reading and
trial and error I came up with the following code which works in the
test worksheet for both chart sheets and embedded charts. It is excerpted
from a sub with parameters for the characteristics of the marker e.g.
SeriesCollectionIndex, ThePoint, XLMarker, ColorIndex). Note, I also
changed the method of specifying the color to refer to the Excel Palette

'
' Select the Point
'
ActiveChart.FullSeriesCollection(SeriesCollectionIndex).Points(ThePoint).Select
'
' Set the marker parameters
'

Selection.MarkerStyle = XLMarker
Selection.MarkerSize = SizeOfMarker

'
Selection.Format.Fill.Visible = msoTrue
'
Selection.MarkerForegroundColorIndex = ColorIndex
Selection.MarkerBackgroundColorIndex = ColorIndex
Selection.Format.Line.Visible = msoFalse

This code works fine in the test workbook. It runs but does nothing
in the Project workbook.

Then I did more random reading in the MS Excel VBA documentation
and discovered that "the MarkerStyle property applies
only to "line chart, scatter chart, or radar chart".

The charts in the test workbook are Line charts and I thought that
the chart in the project workbook was a Line chart, too. However,
that chart is identified by Excel as a "Combo" chart, which is not
defined anywhere that I could find. I re-created the "Combo" chart
in the project workbook and found that it changed from a
Line chart to a Combo chart when I added the secondary y-axis.
Recall that when I used Macro Record, Excel generated code using
the MarkerStyle property! More maddeningly, when the macro code
with MarkerStyle is run for a "Combo" chart, Excel
does not generate an error; it just ignores the whole thing.

So, my question is: Since Excel obviously knows how to put
markers on a series point in a "Combo" chart (because I
can do it manually), how can I do it with VBA?

I would be most appreciative if you can help me with this.

Developer technologies Visual Basic for Applications
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.