ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Charts and Charting in Excel (https://www.excelbanter.com/charts-charting-excel/)
-   -   Setting source data range with Charts (https://www.excelbanter.com/charts-charting-excel/149-setting-source-data-range-charts.html)

D

Setting source data range with Charts
 
I'm trying to figure out how to define the source data range when the range
is not predetermined, when adding a chart in VBA. I can't just do "A1:B3",
because it will be an unknown number of rows (though always two columns). If
cells worked (as I've tried below), I could just use a variable for the
appropriate row reference, but no go so far. Any ideas? Thanks a lot.

With Charts(1)
.Location whe=xlLocationAsNewSheet
.SetSourceData Source:=Sheets("Sheet1").Range(Cells(1, 1),
Cells(2, 2)), PlotBy:=xlColumns
.ChartType = xlBarClustered
End With

William

Hi

Sub ChartRange()
Dim r As Range, c As Chart
With Sheets("Sheet1")
Set r = .Range(.Range("A1"), _
..Range("A" & Rows.Count).End(xlUp).Offset(0, 1))
End With
Set c = Charts.Add
c.Location Whe=xlLocationAsNewSheet, Name:="MyChart"
c.SetSourceData Source:=r, PlotBy:=xlColumns
End Sub


--
XL2002
Regards

William



"D" wrote in message
...
| I'm trying to figure out how to define the source data range when the
range
| is not predetermined, when adding a chart in VBA. I can't just do
"A1:B3",
| because it will be an unknown number of rows (though always two columns).
If
| cells worked (as I've tried below), I could just use a variable for the
| appropriate row reference, but no go so far. Any ideas? Thanks a lot.
|
| With Charts(1)
| .Location whe=xlLocationAsNewSheet
| .SetSourceData Source:=Sheets("Sheet1").Range(Cells(1, 1),
| Cells(2, 2)), PlotBy:=xlColumns
| .ChartType = xlBarClustered
| End With



jeffP

Setting source data range with Charts
 
William: exactly the help I was also looking for. Thanks and Happy New Year
jeff

"William" wrote:

Hi

Sub ChartRange()
Dim r As Range, c As Chart
With Sheets("Sheet1")
Set r = .Range(.Range("A1"), _
..Range("A" & Rows.Count).End(xlUp).Offset(0, 1))
End With
Set c = Charts.Add
c.Location Whe=xlLocationAsNewSheet, Name:="MyChart"
c.SetSourceData Source:=r, PlotBy:=xlColumns
End Sub


--
XL2002
Regards

William



"D" wrote in message
...
| I'm trying to figure out how to define the source data range when the
range
| is not predetermined, when adding a chart in VBA. I can't just do
"A1:B3",
| because it will be an unknown number of rows (though always two columns).
If
| cells worked (as I've tried below), I could just use a variable for the
| appropriate row reference, but no go so far. Any ideas? Thanks a lot.
|
| With Charts(1)
| .Location whe=xlLocationAsNewSheet
| .SetSourceData Source:=Sheets("Sheet1").Range(Cells(1, 1),
| Cells(2, 2)), PlotBy:=xlColumns
| .ChartType = xlBarClustered
| End With





All times are GMT +1. The time now is 07:19 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com