VBA Chart.SetSourceData suddenly stopped working

Orion19

New Member
Joined
Dec 18, 2017
Messages
47
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!!
 

Some videos you may like

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Watch MrExcel Video

Forum statistics

Threads
1,102,890
Messages
5,489,553
Members
407,700
Latest member
SimpleJuan

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top