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:
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:
Any ideas what's going on? Thank you!!
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!!