Question posted 2013 · +4 upvotes
I am creating a report template part of which will be generating data charts with a structure similar to a moving average
for this I need to select a range of data in one sheet (e.g. f10:i14) and place and set it as the source of one chart
The following piece of code works
ActiveChart.SetSourceData Source:=Sheets("Weekly Trends").Range("f10,i14")
but this does not
ActiveChart.SetSourceData Source:=Sheets("Weekly Trends").Range(Cells(x, y), Cells(k, z))
it returns a 1004 error
But I need to specify the range based on variable indices for my report purposes, please help
Accepted answer +10 upvotes
You have to fully qualify the .Cells object
Try this
ActiveChart.SetSourceData Source:=Sheets("Weekly Trends").Range(Sheets("Weekly Trends").Cells(x, y), Sheets("Weekly Trends").Cells(k, Z))
You can also make the above code short
With Sheets("Weekly Trends")
ActiveChart.SetSourceData Source:=.Range(.Cells(x, y), .Cells(k, Z))
End With
Top excel-vba Q&A (6)
- How to clear the entire array? +58 (2010)
- How to change Format of a Cell to Text using VBA +55 (2011)
- Download attachment from Outlook and Open in Excel +43 (2012)
- Can a VBA function in Excel return a range? +36 (2009)
- 2 Dimensional array from range +34 (2013)
- Hiding an Excel worksheet with VBA +33 (2009)
excel-vba solutions on this site
.