Chart Color issues with VBA

XL_rules

New Member
Joined
Jun 16, 2019
Messages
4
I'm creating a very simple chart to show a variety of classroom modalities and how many of each type we are offering. When I run the chart manually I get "The right one.jpg". But when I run it with VBA I get "The wrong one.jpg" (notice that it is all blue and has thinner stacks.) I recorded the following macro by manually creating the chart choosing "The correct chart.jpg." When I stepped through the process while recording "The right one.jpg" was the result. But when I run the macro it results in the "The wrong one.jpg."

Here's the macro I recorded:
Sub Macro1()
Range("A1:I2").Select
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("Charts!$A$1:$I$2")
Range("P6").Select
ActiveSheet.ChartObjects("Chart 1").Activate
End Sub

Here's the data I used and is in my spreadsheet at $A$1:$I$2:
FB (4)HV (36)HY (52)IN (376)IS (9)LO (202)P (52)PI (9)Tot (740)
SMCC
4​
36​
52​
376​
9​
202​
52​
9​
740​
 

Attachments

  • The right one.jpg
    The right one.jpg
    12.6 KB · Views: 7
  • The wrong one.jpg
    The wrong one.jpg
    12.5 KB · Views: 8
  • The correct chart.jpg
    The correct chart.jpg
    39 KB · Views: 7

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
What a bugger. Try this.


VBA Code:
Sub Macro1()
Range("A1:I2").Select
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("Charts!$A$1:$I$2")
ActiveChart.PlotBy = xlColumns
Range("P6").Select
ActiveSheet.ChartObjects("Chart 1").Activate
End Sub
 
Upvote 0
This line corrected the color problems: ActiveChart.PlotBy = xlColumns
Thank you mrshl9898.

Unfortunately it also deletes the x axis.
1622825418810.png


Any suggestion as to how to get that back???
 
Upvote 0
Oh, I didn't experience the issue. Not at a PC til Monday now either. I'll get back to you if no one else replies
 
Upvote 0
Had no luck replicating the issue, hoped someone else would chime in. Try to reset the axis like in the below.

VBA Code:
ActiveChart.Axes(xlCategory). MaximumScale = application.WorksheetFunction.max("A1:I1") 

ActiveChart.Axes(xlCategory). MinimumScale = application.WorksheetFunction.min("A1:I1")
 
Upvote 0

Forum statistics

Threads
1,214,967
Messages
6,122,503
Members
449,090
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top