Charting with VBA - wanting to use .End(xlDown) but getting method failure

markdarby

New Member
Joined
Jun 24, 2018
Messages
1
Hi All,
I've read more web posts and tried more things that I can shake a stick at... none seem to work. I've hard coded the name of the data sheet that I'm using for now... but will revert to using the parameter that I pass to the subroutine later.

Sub CreateGraphs(ProjectToGraph As String)

Dim MyChart As Chart
Dim DataRange As Range


ProjectToGraph = "ARLG"

Worksheets("ARLG").Activate

With Worksheets("ARLG").Shapes.AddChart2(227, xlLineStacked, 30, 30, 600, 400).Chart

'===================================================================================================

' This line works without any issue. Since I'll never know how many rows are in the data field, I would have to edit the range every time I created the chart.
'.SetSourceData Range("ARLG_Data!$A$1:$C$35")

' Wanting to dynamically set the last row.... but this fails every time with an error: "Method 'SetSourceData' of object'_Chart' failed"
.SetSourceData Range("ARLG_Data!$A$1:$C$1"), Range("ARLG_Data!$A$1:$C$1").End(xlDown)




'=================================================================================================== .HasTitle = True
.ChartTitle.Text = ProjectToGraph
.SetElement (msoElementLegendBottom)
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Labor Hours"
.Axes(xlCategory, xlPrimary).TickLabels.NumberFormat = "dd mmm"
End With

' Need to add a vertical line for the date....

End Sub


I'd be grateful for help that anyone can provide.

Mark
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
Code:
.SetSourceData sheets("ARLG_Data").Range("$A$1", Sheets("ARLG_Data").Cells(Rows.Count, 3).End(xlUp))

Or if you have other data below the range you want to use, then

Code:
.SetSourceData sheets("ARLG_Data").Range("$A$1", Sheets("ARLG_Data").Cells(1, 3).End(xlDown))
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,130,029
Messages
5,639,623
Members
417,101
Latest member
amoverton2

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
Top