VBA Chart.SetSourceData suddenly stopped working

Orion19

Board Regular
Joined
Dec 18, 2017
Messages
56
Hi Everyone,

I have been using this block of code for almost a year without any issues. Today I started getting and error that says "Compile error: Method or data member not found" as soon as the code runs. The VBA editor highlights this specific section of code:

Code:
cht.Chart.SetSourceData Source:=rng

I can't access .SetSourceData even after defining cht as a ChartObject. I got the idea for the code here: https://www.thespreadsheetguru.com/blog/2015/3/1/the-vba-coding-guide-for-excel-charts-graph

Here is the full method:

Code:
Public Sub LangSampArticChart()


'PURPOSE: Create a chart (chart dimensions are required)


Dim rng As Range
Dim cht As ChartObject


'Your data range for the chart
  Set rng = ActiveSheet.Range("A30:F35")


'Designates which cell to place the top left corner of the graph in
  ActiveSheet.Range("H25").Select


'Create a chart
  Set cht = ActiveSheet.ChartObjects.Add( _
    Left:=ActiveCell.Left, _
    Width:=360, _
    Top:=ActiveCell.Top, _
    Height:=216)


'Give chart some data
  cht.Chart.SetSourceData Source:=rng
  


'Determine the chart type
  cht.Chart.ChartType = xlColumnClustered
  
'Remove Major Gridlines
  cht.Chart.Axes(xlValue).MajorGridlines.Delete


'Add Data Labels on Outside End
  cht.Chart.SetElement (msoElementDataLabelOutSideEnd)


'Set Maximum Y-Axis Scale and Define Number Format
  With cht.Chart.Axes(xlValue)
      .MaximumScale = 1
      .Crosses = xlCustom
  End With
  cht.Chart.Axes(xlValue).TickLabels.NumberFormat = "0%"


'Sets Gap Width (Column Width)
  cht.Chart.ChartGroups(1).GapWidth = 100


'Sets the legend to the bottom of the graph
  cht.Chart.SetElement (msoElementLegendBottom)


'Colors All Bars Grayscale
  cht.Chart.SeriesCollection(1).Select
  With Selection.Format.Fill
      .Visible = msoTrue
      .ForeColor.ObjectThemeColor = msoThemeColorBackground1
      .ForeColor.TintAndShade = 0
      .ForeColor.Brightness = -0.25
      .Transparency = 0
      .Solid
  End With
  cht.Chart.SeriesCollection(2).Select
  With Selection.Format.Fill
      .Visible = msoTrue
      .ForeColor.ObjectThemeColor = msoThemeColorBackground1
      .ForeColor.TintAndShade = 0
      .ForeColor.Brightness = -0.5
      .Transparency = 0
      .Solid
  End With
  cht.Chart.SeriesCollection(3).Select
  With Selection.Format.Fill
      .Visible = msoTrue
      .ForeColor.ObjectThemeColor = msoThemeColorText1
      .ForeColor.TintAndShade = 0
      .ForeColor.Brightness = 0.35
      .Transparency = 0
      .Solid
  End With
  cht.Chart.SeriesCollection(4).Select
  With Selection.Format.Fill
      .Visible = msoTrue
      .ForeColor.ObjectThemeColor = msoThemeColorText1
      .ForeColor.TintAndShade = 0
      .ForeColor.Brightness = 0
      .Transparency = 0
      .Solid
  End With
cht.Chart.SeriesCollection(5).Select
  With Selection.Format.Fill
      .Visible = msoTrue
      .ForeColor.ObjectThemeColor = msoThemeColorText1
      .ForeColor.TintAndShade = 0
      .ForeColor.Brightness = 0
      .Transparency = 0
      .Solid
  End With


cht.Select
End Sub

Any ideas what's going on? Thank you!!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,213,558
Messages
6,114,297
Members
448,564
Latest member
ED38

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