Black Border around the Column Chart in VBA - vba

I try to set a black border around the Column chart from my Vba CODE. This is what i have now. The last row where i set Border.ColorIndex obviously does not work. Currently the column looks like this.
I want it to look like this.
Here is my code.
ActiveSheet.Cells(10000, 10000).Select
ActiveSheet.Shapes.AddChart.Select
With ActiveChart ' clear SeriesCollection
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
End With
ActiveChart.ChartType = xlColumnClustered
ActiveChart.PlotVisibleOnly = True
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Plottt" ' rename chart sheets
' create SeriesCollection for each line
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "Hallo"
ActiveChart.SeriesCollection(1).XValues = breaks
ActiveChart.SeriesCollection(1).Values = freq
ActiveChart.ChartGroups(1).GapWidth = 0
ActiveChart.ChartGroups(1).Border.ColorIndex = 3
Also i would like to reduce the step size from my code. Help with this would also be appreciated.

To set the border color for a SeriesCollection(1) use the line below:
ActiveChart.SeriesCollection(1).Format.Line.ForeColor.RGB = RGB(255, 0, 0)
Note: it's better to not use ActiveChart, and it's "relatives". Instead use referenced objects. In this case use a ChartObject.
Simple reference code:
Dim Chtobj As ChartObject
' modify "Chart_Data" Name to your Sheet, and "Chart 1" to your chart's name
Set Chtobj = Sheets("Chart_Data").ChartObjects("Chart 1")
With Chtobj
' modify the chartobject properties here...
' modify the major unit of X-axis
.Axes(xlCategory).MajorUnit = 5 '<-- modify to whatever value you want
' modify the minor unit of X-axis
.Axes(xlCategory).MinorUnit = 1
End With

I am not sure why the border isn't showing, though I suspect that there must be a property that determines its visibility or thickness.
For cleaning up your code though, try this:
Dim oChart as Object
ActiveSheet.Cells(10000, 10000).Select
Set oChart = ActiveSheet.Shapes.AddChart
With oChart' clear SeriesCollection
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
.ChartType = xlColumnClustered
.PlotVisibleOnly = True
.Location Where:=xlLocationAsNewSheet, Name:="Plottt" ' rename chart sheets
' create SeriesCollection for each line
.SeriesCollection.NewSeries
With .SeriesCollection(1)
.Name = "Hallo"
.XValues = breaks
.Values = freq
End With
With .ChartGroups(1)
.GapWidth = 0
With .Border
.ColorIndex = 3
' You may need the LineStyle property of the border
' https://msdn.microsoft.com/en-us/library/office/ff821622.aspx
.Linestyle = xlContinuous
End With
End With
End With ' Ends the with block for the entire chart
This should not only clean up your code quite a bit and make it easier to debug, but I have had cases where an object I am working with doesn't update properly if I try to update it's properties outside of a with block. I am not sure if there is a reason for this or not, but I err on the side of using the With block just in case.

Related

Formatting with Charts

I have a sheet and I am generating the Chart from a table.
I would like to generate 2 charts. One chart with absolute numbers and other chart for the same data with Percentage.
Right now, for this, I am using two code, just by adding a line for generating the chart with Y.axis in percentage.
I would like to define column where my chart will start (for eg: chart1 from G7) and chart2 from G15. (I don't have this in my code)
I also, would like to define the length , height and width for my chart.(I don't have this in my code)
It would be great if you can help me add this requirement and do it In a single program.
Sub chartstatus()
Dim rng As Range
Dim cht As Object
Set rng = ActiveSheet.Range("A2:E53")
Set sh = ActiveSheet.Shapes.AddChart
sh.Select
Set cht = ActiveChart
With cht
.SetSourceData Source:=rng
.ChartType = xlColumnClustered
cht.Axes(xlSecondary).TickLabels.NumberFormat = "0.0%"
End With
cht.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 255, 255) '<~~ Red
cht.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
cht.SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(0, 255, 0)
cht.HasTitle = True
cht.ChartTitle.Text = "Result 2017"
End Sub
I use the same code, deleting the line to generate the second chart
cht.Axes(xlSecondary).TickLabels.NumberFormat = "0.0%"
The easier way if to use the ChartObject to create and define the chart, and then modify all it's properties (such as position and dimension).
The code below will create the first chart, place it in Cell "G7", and I modifed it's dimensions to show you the properties you need to modify.
You can add another one for the second chart (with an easy copy>>paste).
Code
Option Explicit
Sub chartstatus()
Dim Rng As Range
Dim ChtObj As ChartObject
Set rng = ActiveSheet.Range("A2:E53")
' use ChartObject instead of shape
Set ChtObj = ActiveSheet.ChartObjects.Add(100, 100, 500, 500) '<-- default dimension and location >> can modify later
With ChtObj
.Chart.ChartType = xlColumnClustered
.Chart.SetSourceData Rng
With .Chart
.Axes(xlSecondary).TickLabels.NumberFormat = "0.0%"
.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 255, 255) '<~~ Red
.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
.SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(0, 255, 0)
.HasTitle = True
.ChartTitle.Text = "Result 2017"
End With
' set position of the chart to Cell G7
.Top = Range("G7").Top
.Left = Range("G7").Left
' change the dimensions of the chart
With .Chart.ChartArea
.Width = 1060
.Height = 420
End With
End With
End Sub
For changing the chart location :
vba to add a shape at a specific cell location in Excel
For chart size:
sh.Width =100
sh.Height =100

VBA (Excel): Run-time error '-2147467259(80004005)': Method 'Height' of object 'PlotArea' failed [duplicate]

I am experiencing an error in a subroutine attempting to set the plotarea.width property of a chart.
The other dimensions also cause this error if I comment out the preceding line(s).
There is no ActiveChart, no selection, etc. The specific error message is this: "-2147467259 (80004005) Method 'Width' of object 'PlotArea' failed"
This is stumping me for several reasons:
In debug mode, F8 to step through the code the error does NOT occur.
AFAIK "width" is not a "method" but a "property" of the chart's plotarea, so even the error message is rather ambiguous.
Any thoughts? Here's as much code as I can share, the ChartSizeMedium subroutine in its entirety, and a dummy snippet to show you how I am establishing the chart and passing it to that sub which sets the size & some other properties prior to passing to another function which adds the series data to the chart.
Option Explicit
Private Sub EstablishChartObject()
Dim cObj as ChartObject
Set cObj = ActiveSheet.ChartObjects.Add(Left:=30, Top:30, Width:=740, Height:=300)
ChartSizeMedium cObj.Chart, "Integer", "Example Chart Title"
End Sub
Private Sub ChartSizeMedium(cht As Chart, NumType As String, Optional chtTitle As String)
'Subroutine to make a consistent size chart
Dim s As Long
With cht
'Add a chart title if one exists.
If Len(chtTitle) > 0 Then
.HasTitle = True
.chartTitle.Characters.Text = chtTitle
End If
'Create the default chart Legend
.HasLegend = True
With .Legend
.Position = xlTop
.Font.Size = 11
.Font.Bold = True
End With
'Format the axes
.Axes(xlValue).MajorGridlines.Format.Line.Visible = msoFalse
.Axes(xlValue).MinorGridlines.Format.Line.Visible = msoFalse
'Format the size of the chart
With .Parent
.Width = 740
.Height = 396
End With
With .PlotArea
.Width = 640 '<---- THIS LINE TRIGGERS THE ERROR
.Height = 280
.Left = 30
.Top = 30
End With
End With
'Some charts start with more than one series container, so make sure they're gone:
With cht
Do Until .SeriesCollection.Count = 0
s = .SeriesCollection.Count
.SeriesCollection(s).Delete
Loop
End With
End Sub
UPDATE Dec 12, 2012
I remove all non-problematic code and use only the PlotArea with block, in the same routine, I have also tried setting the chart type (several values) and as shown in this example, manually adding one series of data prior to attempting to set the PlotArea dimensions, but the error persists:
Option Explicit
Private Sub EstablishChartObject2()
Dim cObj As ChartObject
Dim sh As Worksheet
Set sh = ActiveSheet
Dim srs As Series
Set cObj = sh.ChartObjects.Add(Left:=30, Top:=30, Width:=740, Height:=300)
Set srs = cObj.Chart.SeriesCollection.NewSeries
srs.Values = "={1,3,5,7,4}"
cObj.Chart.ChartType = 57
With cObj.Chart.PlotArea
.Width = 100 '<---- THIS LINE TRIGGERS THE ERROR
.Height = 280
.Left = 30
.Top = 30
End With
End Sub
i had a similar problem . And its definitely an excel issue (having 2013).
With .PlotArea
.Select 'err if delete this line of code
.Top = 0
.Left = 0
.width = 40
.Height = 40
End With
if you remove the .selectline, it will result in error on the next line.
note that i am not working with a <with selectiondo stuff>.
the .selectmakes it work, without using the selection, wich is obviously an excel bug (from previous versions?)
Two solutions that seem to be working, neither is really as "elegant" as I'd prefer (I was hoping there would be a way to do this with selecting the chart or any part of it).
Option 1 - Select the plot area and then deselect it. This seems to be the most reliable/efficient solution.
With .PlotArea
Application.ScreenUpdating = False
.Select
With Selection
.Width = paWidth
.Height = paHeight
.Left = paLeft
.Top = paTop
ActiveSheet.Range("A1").Activate
End With
Application.ScreenUpdating = True
End With
Option 2 - disable error-handling in loop (this followed from Doug's link). This doesn't seem to be a very reliable or efficient method, and although it seems to work, I know that within that loop it is failing once on each of the properties before it successfully sets them on a subsequent pass.
With .PlotArea
For pLoop = 1 To 5
On Error Resume Next
.Width = paWidth
.Height = paHeight
.Left = paLeft
.Top = paTop
On Error GoTo 0
Next
End With
Hope your sheet and chart has the ability to get a width up to 640. If so try the explicit reference. Also suggest you to change width, height values to lower values and see how programme responds. Since you said, when you select it works,
that also means, your cht is wrapping the correct chart object - unless otherwise you selected the chart using ActiveChart.PlotArea.Width . Hence I guess explicit reference could do be a potential try out.
,
cht.PlotArea.Width = 640
cht.PlotArea.Height = 280
cht.PlotArea.Left = 30
cht.PlotArea.Top = 30
Further, check on Aspect Ratio lock or unlock. If none of these works, then add a chart into your sheet and use most simple chart formatting code to check on widht, height, left, top changes.
Update two
Let's try specifing chart type and setting up chart object in the second sub as well. I tried out in my end and it's working. Try the code with following changes.
Code:
Calling this sub from the button showed in the sheet.
Option Explicit
Public Sub EstablishChartObject()
Dim mySheet As Worksheet
Dim cObj As ChartObject
Application.ScreenUpdating = False
Application.StatusBar = "Chart is coming soon..."
Set mySheet = Sheets(2) '-- set according to yours
'-- create chart with some source data first, which you can change later
Set cObj = mySheet.ChartObjects.Add(Left:=30, Top:=30, Width:=400, Height:=200)
ChartSizeMedium cObj, "Integer", "Example Chart Title"
End Sub
'Subroutine to make a consistent size chart
Private Sub ChartSizeMedium(chtObj As ChartObject, NumType As String, _
Optional chtTitle As String)
Dim myChart As Chart
Dim s As Long
Set myChart = chtObj.Chart '-- specify chart type
myChart.SetSourceData Source:=Sheets(2).Range("B3:C12") '-- set to what you have
myChart.ChartType = xlXYScatterLines '-- set to the type you want
'and make sure to **use correct properties**
With myChart
If .HasTitle Then
.ChartTitle.Characters.Text = chtTitle
End If
'Create the default chart Legend
If .HasLegend Then
With .Legend
.Position = xlTop
.Font.Size = 11
.Font.Bold = True
End With
End If
'Format the axes
With .Axes(xlValue)
.HasMajorGridlines = False
End With
'Format the size of the chart
With .Parent
.Width = 400 '-- change to yours
.Height = 250 '-- change to yours
End With
With .PlotArea
.Width = 300 '-- change to yours
.Height = 180 '-- change to yours
.Left = 30
.Top = 30
End With
End With
Application.ScreenUpdating = True
Application.StatusBar = "Chart is Here!"
End Sub
Otput:
Make sure to use correct properties for each chart type. Note that above code doesn't delete any left over, old charts from your sheet.
.MajoreGridlines.Format.Lines.Visible fails. So set the .MajorGridlines = False to make sure you do not want to show the gridlines.
Anything else you want to do can be done later. Just try with the changes to dimension initially.
Reference from : MSDN Gridlines property
I know this is old, and this solution seems bad, but it works. I only thought to do it as you mention that stepping through works.
Option Explicit
Sub chart()
Dim cObj As ChartObject
Dim sh As Worksheet
Set sh = ActiveSheet
Dim srs As Series
Set cObj = sh.ChartObjects.Add(Left:=30, Top:=30, Width:=740, Height:=300)
cObj.chart.ChartType = 57
Set srs = cObj.chart.SeriesCollection.NewSeries
srs.Values = "={1,3,5,7,4}"
Application.Wait Now + TimeValue("00:00:01") '<~~ Added This line
With cObj.chart.PlotArea
.Width = 100
.Height = 280
.Left = 30
.Top = 30
End With
End Sub
EDIT: This seems to work for some chart types, but it was still failing for other chart types. I have continued to use the 5x loop with On Error Resume Next and that seems to be -- unfortunately -- the most "reliable" solution to-date.
Original: This is based on User2140261's suggested answer, above:
https://stackoverflow.com/a/16041640/1467082
Since the question was initially posted, the application now resides in PowerPoint, so I cannot use the Applicaiton.Wait. I had some intermittent errors with a 1-second pause, and 3-seconds was too much of a pause, so I built the following error trap. The same idea could be used in Excel in conjunction with Application.Wait.
It was this block of code that was giving me fits, so I added this error handling in Powerpoint to mimic the Application.Wait.
RetryChartDimensions:
On Error GoTo ErrChartDimensions
With .PlotArea
.Width = paWidth
.Height = paHeight
.Left = paLeft
.Top = paTop
End With
On Error GoTo 0
' More code
' more code
Exit Sub 'gracefully exit this subroutine before the error-handling.'
ErrChartDimensions:
Err.Clear
'Pause before setting the PlotArea dimensions:'
Dim wtTime As Double
Dim startTime As Long
'A maximum 3 second delay should be more than enough time.
If wtTime < 3 Then
wtTime = wtTime + 0.5
startTime = Timer
While Timer < startTime + wtTime
DoEvents
Wend
End If
Resume RetryChartDimensions
End Sub
I don't have enough reputation to add a comment, so using the above solutions I have fixed my problem with Pie Charts in VB.Net 2010 and Excel 2013. xlLine charts never caused a problem, but my code would crash when the same code was ran against an xlPie chart on Excel 2013 (All was fine on Excel 2007).
My now working code:
appExcel.Visible = False
xlchart_for_96_Well_Plate_Source = appExcel.Charts.Add(After:=wkbExperiment_Details.Sheets(wkbExperiment_Details.Sheets.Count))
appExcel.ScreenUpdating = False
With xlchart_for_96_Well_Plate_Source
.SetSourceData(Source:=wksData.Range(wksData.Cells(2, byteCharts_added), wksData.Cells(intUsed_Rows, byteCharts_added)), PlotBy:=Microsoft.Office.Interop.Excel.XlRowCol.xlColumns)
.ChartType = objChart_Type
.PlotArea.Select()
.PlotArea.Top = 2
.PlotArea.Select()
.PlotArea.Left = 2
.SeriesCollection(.SeriesCollection.count).xvalues = wksData.Range(wksData.Cells(2, 1), wksData.Cells(intUsed_Rows, 1)).Value ' Scale - wavelength for line chart
.SeriesCollection(.SeriesCollection.count).Values = wksData.Range(wksData.Cells(2, byteCharts_added + 1), wksData.Cells(intUsed_Rows, byteCharts_added + 1)).Value
.SeriesCollection(.SeriesCollection.count).Name = wksData.Cells(1, .SeriesCollection.count + 1).value
End With
appExcel.ScreenUpdating = True

Why are some series temporarily missing from my Excel chart?

I am producing an Excel column chart from a VB macro. I added 3 horizontal reference lines and find that when the macro completes, only the first one appears (along with the column chart). However, if I save the workbook and reopen it, all 3 reference lines are visible. Alternatively, if I go to the design tab to "Select Data" and simply click on any one of the reference line names, all 3 immediately appear. Any ideas on what I can put in my macro to get them all to appear automatically when the macro completes?
The code that creates these 3 reference lines is (sc is the chart's SeriesCollection):
With sc.NewSeries
.ChartType = xlXYScatterLinesNoMarkers
.Name = "A"
.XValues = "={1,3}"
.Values = "={100,100}"
End With
With sc.NewSeries
.ChartType = xlXYScatterLinesNoMarkers
.Name = "B"
.XValues = "={1, 3}"
.Values = "={80, 80}"
End With
With sc.NewSeries
.ChartType = xlXYScatterLinesNoMarkers
.Name = "C"
.XValues = "={1, 3}"
.Values = "={50, 50}"
End With
EDIT: Here is a screenshot using the data in the answer by #axel-richter. This is what it looks like immediately after the macro ends.
I suppose this is with Excel 2010 and later.
Call Chart.ChartWizard Method without parameters after you added new series.
Example:
Suppose we have:
Then after running this macro:
Sub addChart()
Dim oChart As ChartObject
Dim sc As SeriesCollection
Set oChart = ActiveSheet.ChartObjects.Add(300, 40, 300, 200)
oChart.Chart.ChartWizard Source:=ActiveSheet.Range("A1:D4"), Gallery:=xlColumn
Set sc = oChart.Chart.SeriesCollection
With sc.NewSeries
.ChartType = xlXYScatterLinesNoMarkers
.Name = "A"
.XValues = "={1,3}"
.Values = "={100,100}"
End With
With sc.NewSeries
.ChartType = xlXYScatterLinesNoMarkers
.Name = "B"
.XValues = "={1, 3}"
.Values = "={80, 80}"
End With
With sc.NewSeries
.ChartType = xlXYScatterLinesNoMarkers
.Name = "C"
.XValues = "={1, 3}"
.Values = "={50, 50}"
End With
oChart.Chart.ChartWizard
End Sub
we have:
I still have no explanation for why those lines don't show up for me, but I've found a "solution" that coerces them into visibility. I added the following at the end of the macro to (a) add an extra series at the end, and (b) immediately delete it.
With sc.NewSeries
.ChartType = xlXYScatterLinesNoMarkers
.Name = "D"
.XValues = "={1, 3}"
.Values = "={50, 50}"
End With
sc(sc.Count).Select
Application.SendKeys "{Delete}"
Note the last line. Using Selection.Delete deletes the extra series but the lines that were invisible stay that way.
If you've got the time try not reuse your SeriesCollection object.
I ran into this with a C# app I'm writing. The only similarity I could see between our code was that we were both calling NewSeries() on the same SeriesCollection object.
It's also worth noting the Chartwizard, and quick-series-create-delete workaround methods didn't work for me.
What did work was calling seriescollection() on the chart object before each SeriesCollection.NewSeries() call.
As in:
ChartObject co = target.ChartObjects().Item(1);
Chart c = co.Chart;
SeriesCollection s = c.SeriesCollection();
Series s1 = s.NewSeries();
s1.ChartType = XlChartType.xlLine;
s = c.SeriesCollection();
Series s2 = s.NewSeries();
s2.ChartType = XlChartType.xlLine;
Total conjecture, but I suspect a defect in the SeriesCollection class; potentially some event isn't firing on subsequent NewSeries calls. Depending on the version each of us is using the workarounds were causing the event that isn't firing to fire and therefore we would see the invisible series' actually appear.

mulit plot from different sheets in one chart with VBA

My problem is that I want to make many graph in one chart but the data is from different sheets.
At the moment my code can only take multi data from one sheet, meaning I can plot 2 graph from one sheet.
My code at the moment is:
Sub ChartSheet()
Dim ChartSheet1 As Chart
Set ChartSheet1 = Charts.Add
With ChartSheet1
.SetSourceData Source:=Sheets("Sheet1").Range("E12:E6232, Y12:Y6232")
.ChartType = xlLine
.HasTitle = True
.ChartTitle.Characters.Text = "Test Chart"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "x"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "y"
End With
End Sub
What I want is to say:
.SetSourceData Source:=Sheets("Sheet1").Range("E12:E6232, Y12:Y6232")
.SetSourceData Source:=Sheets("Sheet2").Range("D12:E23")
.SetSourceData Source:=Sheets("Sheet3").Range("Y12:Y6232, G27:G496, H3:5977")
and so on..
But when I do this my code it only print the last line from .SetSoureData
Hope some of you can help me work around this, Many thank in advarnce :)
Update:
I found abit of a work around by looping but this is not my total answer
But here is my other code:
Sub MultiSheetPlot()
Dim cht As Chart, s As Series, xRng As Range
Dim i As Long, chartName As String
Set cht = Charts.Add
cht.ChartType = xlLine
For i = 1 To 3
chartName = "Sheet" & i
Set xRng = Sheets("Sheet1").Range("A1:A20, C1:C20")
With cht.SeriesCollection.NewSeries()
.Values = xRng
.Name = chartName
End With
Next i
End Sub
the problem in this code is that it ignores the last range I define like the C10:C20
Responding to your latest update, I have a Sub that creates a series each time it's called from MultiSheetPlot Sub.
You can actually add more parameters to this Sub (as long as you remeber to pass them in the Calling).
Option Explicit
Dim cht As Chart
Sub MultiSheetPlot()
Set cht = Charts.Add
cht.ChartType = xlLine
' call the series creation chart function (each time for each series you want to add to the existing chart
Call Create_SeriesChart("Sheet1", Sheets("Sheet1").Range("E12:E6232"), 1, True, msoThemeColorText1)
Call Create_SeriesChart("Sheet1", Sheets("Sheet1").Range("Y12:Y6232"), 1, True, msoThemeColorText1)
End Sub
' ------ this Sub creates a series to the chart, it receives the following parameters: ------
' 1. seriesName - String
' 2. serValues - Range
' 3. lineWeight - Double (the weight of the line)
' 4. lineVis - Boolean (if you want to hide a certail series)
' 5. lineColor - MsoColorType (using the current's PC Theme colors
Sub Create_SeriesChart(seriesName As String, serValues As Range, lineWeight As Double, lineVis As Boolean, lineColor As MsoColorType)
Dim Ser As Series
Set Ser = cht.SeriesCollection.NewSeries
With Ser
.Name = seriesName
.Values = serValues
.Format.Line.Weight = lineWeight
If lineVis = True Then
.Format.Line.Visible = msoTrue
Else
.Format.Line.Visible = msoFalse
End If
.Format.Line.ForeColor.ObjectThemeColor = lineColor ' Line color Black
End With
End Sub
My solution is use Name of plage, you can give a name of your range
Like in my photo :
Everytime when you want to use these datas you just need call them
Like this plage i gived them name Poste_EtatDeLaDemande
When i want to use these elements i just call it like Range("Poste_EtatDeLaDemande") Excel will find it you don't need tell them where it is anymore ; )

Error setting PlotArea.Width in Excel, VBA (Excel 2010)

I am experiencing an error in a subroutine attempting to set the plotarea.width property of a chart.
The other dimensions also cause this error if I comment out the preceding line(s).
There is no ActiveChart, no selection, etc. The specific error message is this: "-2147467259 (80004005) Method 'Width' of object 'PlotArea' failed"
This is stumping me for several reasons:
In debug mode, F8 to step through the code the error does NOT occur.
AFAIK "width" is not a "method" but a "property" of the chart's plotarea, so even the error message is rather ambiguous.
Any thoughts? Here's as much code as I can share, the ChartSizeMedium subroutine in its entirety, and a dummy snippet to show you how I am establishing the chart and passing it to that sub which sets the size & some other properties prior to passing to another function which adds the series data to the chart.
Option Explicit
Private Sub EstablishChartObject()
Dim cObj as ChartObject
Set cObj = ActiveSheet.ChartObjects.Add(Left:=30, Top:30, Width:=740, Height:=300)
ChartSizeMedium cObj.Chart, "Integer", "Example Chart Title"
End Sub
Private Sub ChartSizeMedium(cht As Chart, NumType As String, Optional chtTitle As String)
'Subroutine to make a consistent size chart
Dim s As Long
With cht
'Add a chart title if one exists.
If Len(chtTitle) > 0 Then
.HasTitle = True
.chartTitle.Characters.Text = chtTitle
End If
'Create the default chart Legend
.HasLegend = True
With .Legend
.Position = xlTop
.Font.Size = 11
.Font.Bold = True
End With
'Format the axes
.Axes(xlValue).MajorGridlines.Format.Line.Visible = msoFalse
.Axes(xlValue).MinorGridlines.Format.Line.Visible = msoFalse
'Format the size of the chart
With .Parent
.Width = 740
.Height = 396
End With
With .PlotArea
.Width = 640 '<---- THIS LINE TRIGGERS THE ERROR
.Height = 280
.Left = 30
.Top = 30
End With
End With
'Some charts start with more than one series container, so make sure they're gone:
With cht
Do Until .SeriesCollection.Count = 0
s = .SeriesCollection.Count
.SeriesCollection(s).Delete
Loop
End With
End Sub
UPDATE Dec 12, 2012
I remove all non-problematic code and use only the PlotArea with block, in the same routine, I have also tried setting the chart type (several values) and as shown in this example, manually adding one series of data prior to attempting to set the PlotArea dimensions, but the error persists:
Option Explicit
Private Sub EstablishChartObject2()
Dim cObj As ChartObject
Dim sh As Worksheet
Set sh = ActiveSheet
Dim srs As Series
Set cObj = sh.ChartObjects.Add(Left:=30, Top:=30, Width:=740, Height:=300)
Set srs = cObj.Chart.SeriesCollection.NewSeries
srs.Values = "={1,3,5,7,4}"
cObj.Chart.ChartType = 57
With cObj.Chart.PlotArea
.Width = 100 '<---- THIS LINE TRIGGERS THE ERROR
.Height = 280
.Left = 30
.Top = 30
End With
End Sub
i had a similar problem . And its definitely an excel issue (having 2013).
With .PlotArea
.Select 'err if delete this line of code
.Top = 0
.Left = 0
.width = 40
.Height = 40
End With
if you remove the .selectline, it will result in error on the next line.
note that i am not working with a <with selectiondo stuff>.
the .selectmakes it work, without using the selection, wich is obviously an excel bug (from previous versions?)
Two solutions that seem to be working, neither is really as "elegant" as I'd prefer (I was hoping there would be a way to do this with selecting the chart or any part of it).
Option 1 - Select the plot area and then deselect it. This seems to be the most reliable/efficient solution.
With .PlotArea
Application.ScreenUpdating = False
.Select
With Selection
.Width = paWidth
.Height = paHeight
.Left = paLeft
.Top = paTop
ActiveSheet.Range("A1").Activate
End With
Application.ScreenUpdating = True
End With
Option 2 - disable error-handling in loop (this followed from Doug's link). This doesn't seem to be a very reliable or efficient method, and although it seems to work, I know that within that loop it is failing once on each of the properties before it successfully sets them on a subsequent pass.
With .PlotArea
For pLoop = 1 To 5
On Error Resume Next
.Width = paWidth
.Height = paHeight
.Left = paLeft
.Top = paTop
On Error GoTo 0
Next
End With
Hope your sheet and chart has the ability to get a width up to 640. If so try the explicit reference. Also suggest you to change width, height values to lower values and see how programme responds. Since you said, when you select it works,
that also means, your cht is wrapping the correct chart object - unless otherwise you selected the chart using ActiveChart.PlotArea.Width . Hence I guess explicit reference could do be a potential try out.
,
cht.PlotArea.Width = 640
cht.PlotArea.Height = 280
cht.PlotArea.Left = 30
cht.PlotArea.Top = 30
Further, check on Aspect Ratio lock or unlock. If none of these works, then add a chart into your sheet and use most simple chart formatting code to check on widht, height, left, top changes.
Update two
Let's try specifing chart type and setting up chart object in the second sub as well. I tried out in my end and it's working. Try the code with following changes.
Code:
Calling this sub from the button showed in the sheet.
Option Explicit
Public Sub EstablishChartObject()
Dim mySheet As Worksheet
Dim cObj As ChartObject
Application.ScreenUpdating = False
Application.StatusBar = "Chart is coming soon..."
Set mySheet = Sheets(2) '-- set according to yours
'-- create chart with some source data first, which you can change later
Set cObj = mySheet.ChartObjects.Add(Left:=30, Top:=30, Width:=400, Height:=200)
ChartSizeMedium cObj, "Integer", "Example Chart Title"
End Sub
'Subroutine to make a consistent size chart
Private Sub ChartSizeMedium(chtObj As ChartObject, NumType As String, _
Optional chtTitle As String)
Dim myChart As Chart
Dim s As Long
Set myChart = chtObj.Chart '-- specify chart type
myChart.SetSourceData Source:=Sheets(2).Range("B3:C12") '-- set to what you have
myChart.ChartType = xlXYScatterLines '-- set to the type you want
'and make sure to **use correct properties**
With myChart
If .HasTitle Then
.ChartTitle.Characters.Text = chtTitle
End If
'Create the default chart Legend
If .HasLegend Then
With .Legend
.Position = xlTop
.Font.Size = 11
.Font.Bold = True
End With
End If
'Format the axes
With .Axes(xlValue)
.HasMajorGridlines = False
End With
'Format the size of the chart
With .Parent
.Width = 400 '-- change to yours
.Height = 250 '-- change to yours
End With
With .PlotArea
.Width = 300 '-- change to yours
.Height = 180 '-- change to yours
.Left = 30
.Top = 30
End With
End With
Application.ScreenUpdating = True
Application.StatusBar = "Chart is Here!"
End Sub
Otput:
Make sure to use correct properties for each chart type. Note that above code doesn't delete any left over, old charts from your sheet.
.MajoreGridlines.Format.Lines.Visible fails. So set the .MajorGridlines = False to make sure you do not want to show the gridlines.
Anything else you want to do can be done later. Just try with the changes to dimension initially.
Reference from : MSDN Gridlines property
I know this is old, and this solution seems bad, but it works. I only thought to do it as you mention that stepping through works.
Option Explicit
Sub chart()
Dim cObj As ChartObject
Dim sh As Worksheet
Set sh = ActiveSheet
Dim srs As Series
Set cObj = sh.ChartObjects.Add(Left:=30, Top:=30, Width:=740, Height:=300)
cObj.chart.ChartType = 57
Set srs = cObj.chart.SeriesCollection.NewSeries
srs.Values = "={1,3,5,7,4}"
Application.Wait Now + TimeValue("00:00:01") '<~~ Added This line
With cObj.chart.PlotArea
.Width = 100
.Height = 280
.Left = 30
.Top = 30
End With
End Sub
EDIT: This seems to work for some chart types, but it was still failing for other chart types. I have continued to use the 5x loop with On Error Resume Next and that seems to be -- unfortunately -- the most "reliable" solution to-date.
Original: This is based on User2140261's suggested answer, above:
https://stackoverflow.com/a/16041640/1467082
Since the question was initially posted, the application now resides in PowerPoint, so I cannot use the Applicaiton.Wait. I had some intermittent errors with a 1-second pause, and 3-seconds was too much of a pause, so I built the following error trap. The same idea could be used in Excel in conjunction with Application.Wait.
It was this block of code that was giving me fits, so I added this error handling in Powerpoint to mimic the Application.Wait.
RetryChartDimensions:
On Error GoTo ErrChartDimensions
With .PlotArea
.Width = paWidth
.Height = paHeight
.Left = paLeft
.Top = paTop
End With
On Error GoTo 0
' More code
' more code
Exit Sub 'gracefully exit this subroutine before the error-handling.'
ErrChartDimensions:
Err.Clear
'Pause before setting the PlotArea dimensions:'
Dim wtTime As Double
Dim startTime As Long
'A maximum 3 second delay should be more than enough time.
If wtTime < 3 Then
wtTime = wtTime + 0.5
startTime = Timer
While Timer < startTime + wtTime
DoEvents
Wend
End If
Resume RetryChartDimensions
End Sub
I don't have enough reputation to add a comment, so using the above solutions I have fixed my problem with Pie Charts in VB.Net 2010 and Excel 2013. xlLine charts never caused a problem, but my code would crash when the same code was ran against an xlPie chart on Excel 2013 (All was fine on Excel 2007).
My now working code:
appExcel.Visible = False
xlchart_for_96_Well_Plate_Source = appExcel.Charts.Add(After:=wkbExperiment_Details.Sheets(wkbExperiment_Details.Sheets.Count))
appExcel.ScreenUpdating = False
With xlchart_for_96_Well_Plate_Source
.SetSourceData(Source:=wksData.Range(wksData.Cells(2, byteCharts_added), wksData.Cells(intUsed_Rows, byteCharts_added)), PlotBy:=Microsoft.Office.Interop.Excel.XlRowCol.xlColumns)
.ChartType = objChart_Type
.PlotArea.Select()
.PlotArea.Top = 2
.PlotArea.Select()
.PlotArea.Left = 2
.SeriesCollection(.SeriesCollection.count).xvalues = wksData.Range(wksData.Cells(2, 1), wksData.Cells(intUsed_Rows, 1)).Value ' Scale - wavelength for line chart
.SeriesCollection(.SeriesCollection.count).Values = wksData.Range(wksData.Cells(2, byteCharts_added + 1), wksData.Cells(intUsed_Rows, byteCharts_added + 1)).Value
.SeriesCollection(.SeriesCollection.count).Name = wksData.Cells(1, .SeriesCollection.count + 1).value
End With
appExcel.ScreenUpdating = True