• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Charts background and font color depending to the color write inside the chart

begins

New Member
Hello to everybody,
I'm new in this spectacular forum!
I'm trying to create a VBA macro that change the background and font color of a chart, and the color is write inside the chart in RGB format, for example like this: RGB(255, 0, 0).

Is it possible?

Thank you very much

Wish you good Sunday
 
Begins

Firstly, Welcome to the Chandoo.org Forums

Yes, You can alter most aspects of charts by using macros in VBA

The best place to start is to setup a chart and the simply edit it whilst recording a macro

You will end up with some code like:

Code:
Sub Macro1()

    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.FullSeriesCollection(1).Select
    With Selection.Format.Fill
      .Visible = msoTrue
      .ForeColor.RGB = RGB(255, 0, 0)
      .Transparency = 0
      .Solid
    End With
    ActiveChart.PlotArea.Select
    With Selection.Format.Fill
      .Visible = msoTrue
      .ForeColor.RGB = RGB(0, 176, 80)
      .Transparency = 0
      .Solid
    End With
    ActiveChart.ChartArea.Select
    With ActiveSheet.Shapes("Chart 1").Fill
      .Visible = msoTrue
      .ForeColor.RGB = RGB(0, 112, 192)
      .Transparency = 0
      .Solid
    End With
    ActiveChart.ChartTitle.Select
    With Selection.Format.TextFrame2.TextRange.Font.Fill
      .Visible = msoTrue
      .ForeColor.RGB = RGB(255, 0, 0)
      .Transparency = 0
      .Solid
    End With
End sub

You can read the code and it mostly identifies what it is doing

eg:
ActiveChart.PlotArea.Select
With Selection.Format.Fill

shows you are formatting the Plotarea

You can simplify this code such as:
Code:
Sub Macro2()

    With ActiveSheet
.ChartObjects("Chart 1").Activate
    .FullSeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 255, 0)
    .PlotArea.Format.Fill.ForeColor.RGB = RGB(0, 176, 100)
    .Shapes("Chart 1").Fill.ForeColor.RGB = RGB(0, 112, 102)
    .ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 255)
End with
End Sub

The biggest hangup with this is that the full object model is not available to you in VBA, that is you will find every now and then actions that cannot be completed in VBA because the Object Model hasn't been fully exposed to VBA

In these cases you have limited opportunities to get around the uissues

Some times you will also get code that after being recorded will not actually work

Google is your friend here, simply search for how others have got around these issues

Of course you are always welcome to come back here for assistance when you get stuck
 
Back
Top