AAhrenst
08-25-2012, 09:42 AM
Hi
I am trying to add a excel chart from a word template which is causing me a lot of frustration. Code seems to be simple enough but for some reason I just cannot make it work. I got serveral problems:
1: cannot edit ChartArea.Width/Height. I get following message "Object doesn't support this function". The PlotArea.Width/Height I am able to set.
2: I am not able to set the series using cell row and column no reference ie. Range(cells(x,y),Cells(z,y). I really need this but for now I am limited to using following: .SeriesCollection(i).Values = "='Sheet1'!$N$2:$N$52"
3: Setting specific color to each line is a problem too:
- I have tried using: .SeriesCollection(i).Border.ColorIndex = xx (which seem to set all all lines the same color
- nothing happens on this code: .SeriesCollection(i).Interior.Color = RGB(xxx, yyy, zzz)
Any feedback will be much appriciated...
below is the code I am using:
-----------------------------------------------------------------
Sub createchart()
Dim WellSecurityChart As Chart
Dim ChartWorkSheet As Excel.Worksheet
Set WellSecurityChart = ActiveDocument.Shapes.AddChart.Chart
Set ChartWorkSheet = WellSecurityChart.ChartData.Workbook.WorkSheets(1)
With WellSecurityChart
.ChartType = xlXYScatterLinesNoMarkers
.PlotArea.Width = 300 ' number not important now
.PlotArea.Height = 400 ' number not important now
.ChartArea.Width = 400 ' number not important now
.ChartArea.Height = 500 ' number not important now
.SeriesCollection(3).Delete 'deleting default series as automatically setup on creation of chart
.SeriesCollection(2).Delete
.SeriesCollection(1).Delete
'....more setup
.SeriesCollection.NewSeries
With .SeriesCollection(1)
.Name = "='Sheet1'!$F$1"
.XValues = "='Sheet1'!$H$2:H$52" ' works but I would prefer to use Range(Cells(x,y,),(Cells(z,i))
.Values = "='Sheet1'!$F$2:$F$52" ' data added elsewhere in code
'.Border.ColorIndex = 3 'set all lines to red
'.Interior.Color = RGB(255, 0, 0) ' does not work
'.Format.Line.ForeColor.RGB = vbRed 'does not work
End With
'......adding more series....
End With
end sub
I am trying to add a excel chart from a word template which is causing me a lot of frustration. Code seems to be simple enough but for some reason I just cannot make it work. I got serveral problems:
1: cannot edit ChartArea.Width/Height. I get following message "Object doesn't support this function". The PlotArea.Width/Height I am able to set.
2: I am not able to set the series using cell row and column no reference ie. Range(cells(x,y),Cells(z,y). I really need this but for now I am limited to using following: .SeriesCollection(i).Values = "='Sheet1'!$N$2:$N$52"
3: Setting specific color to each line is a problem too:
- I have tried using: .SeriesCollection(i).Border.ColorIndex = xx (which seem to set all all lines the same color
- nothing happens on this code: .SeriesCollection(i).Interior.Color = RGB(xxx, yyy, zzz)
Any feedback will be much appriciated...
below is the code I am using:
-----------------------------------------------------------------
Sub createchart()
Dim WellSecurityChart As Chart
Dim ChartWorkSheet As Excel.Worksheet
Set WellSecurityChart = ActiveDocument.Shapes.AddChart.Chart
Set ChartWorkSheet = WellSecurityChart.ChartData.Workbook.WorkSheets(1)
With WellSecurityChart
.ChartType = xlXYScatterLinesNoMarkers
.PlotArea.Width = 300 ' number not important now
.PlotArea.Height = 400 ' number not important now
.ChartArea.Width = 400 ' number not important now
.ChartArea.Height = 500 ' number not important now
.SeriesCollection(3).Delete 'deleting default series as automatically setup on creation of chart
.SeriesCollection(2).Delete
.SeriesCollection(1).Delete
'....more setup
.SeriesCollection.NewSeries
With .SeriesCollection(1)
.Name = "='Sheet1'!$F$1"
.XValues = "='Sheet1'!$H$2:H$52" ' works but I would prefer to use Range(Cells(x,y,),(Cells(z,i))
.Values = "='Sheet1'!$F$2:$F$52" ' data added elsewhere in code
'.Border.ColorIndex = 3 'set all lines to red
'.Interior.Color = RGB(255, 0, 0) ' does not work
'.Format.Line.ForeColor.RGB = vbRed 'does not work
End With
'......adding more series....
End With
end sub