PDA

View Full Version : Change Graph data range



CZENHH
04-18-2011, 01:06 PM
I want the macro to change a graph's data range to reflect all the x inputs and y outputs, which I adjust on the spreadsheet by changing cells(3,2).

This of course does not work:
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$5:adj")

from:


Sub func_1()

Dim xrange As Integer
Dim adj As Range
xrange = Cells(3, 2)

Application.ScreenUpdating = False

Range("A6:B500").ClearContents

Cells(6, 1).Select

For i = 0 To xrange
ActiveCell.Value = i
ActiveCell.Offset(0, 1).Value = i ^ 2
ActiveCell.Offset(1, 0).Select
Next i

Cells(6 + xrange, 2).Select
Set adj = ActiveCell


ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.Axes(xlCategory).MaximumScale = xrange

ActiveChart.SetSourceData Source:=Range("Sheet1!$A$5:adj")

End Sub

How can I do this?
Thanks!

draco664
04-18-2011, 06:51 PM
Try ActiveChart.SetSourceData Source:=Range("Sheet1!$A$5:" & adj.Address(True, True))

CZENHH
04-18-2011, 07:26 PM
Awesome! Thank you.