Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can anybody see what I have left out?
The following code extracts succeed in successfully placing a ChartObject on the right Worksheet, which appears to have picked up the data series and, if the First Failure is commented out, the correct Chart type.

However, nothing I have managed to do allows me to set the Chart Title or to make the beastly thing actually appear!

xlApp is the Excel application 
xlWB is the Excel Workbook 
xlWS has been declared as Object 
xlChartSheet has been declared as object and set as the active sheet 
oRange has been declared as Object 
sCalcSheetName, sRowAddress, sLeftRowCell, sRightRowCell, sRowName have been declared as a string 


Relevant lines of code are as follows:

VB
xlChartSheet.ChartObjects.Add(Left:=100, Width:=375, Top:=75, Height:=225) 
(Commented out)

Code below reads the data series of the Chart, one series at a time, finds the Worksheet on which the row appears, reads its address, gets the left and right cells of the range, creates a Range object which is added to the Charts SeriesCollection, as follows

VB
While sContent.Length is greater than 0 
... 
xlApp.Goto(sRowName) 
xlWS = xlWB.ActiveSheet sCalcSheetName = xlWS.Name 
xlRange = xlWS.Range(sRowName) sRowAddress = xlRange.Address 
sLeftRowCell = xxx 
(string to the left of the colon) 
sRightRowCell = yyy (string to the right of the colon) oRange = xlWS.Range(sLeftRowCell, sRightRowCell) xlChartSheet.ChartObjects(1).Chart.SeriesCollection.Add(oRange) ...
End While 


(Question) All of which seems to be working, but it never reaches either of the two snippets below;

Can anyone spot what have I left out?
(First failure)

With the following, throws up Unable to set the HasAxis property of the Chart Class

VB
With xlChartSheet.ChartObjects(1).Chart 
.HasAxis(1, 1) = False 
.HasAxis(2, 1) = False 
End With 

(Second failure)

Without the above, throws up Unable to set the HasTitle property of the Chart Class

VB
With xlChartSheet.ChartObjects(1).Chart 
.ChartType = 4 
.HasLegend = False 
.HasTitle = True 
End With 


(NB)

xlConstants in the two snippets above have been replaced with their numeric values ...
Posted
Updated 28-May-15 5:45am
v2
Comments
Tony Richards 18-May-10 4:53am    
You seriously need to reformat that question. It is next to impossible to read like that, and a lot of people won't bother. Split it in to paragraphs, stick <pre> tags and <code> tags around you code snippets, and you might get more success.
jk0391 28-May-15 15:25pm    
Please re-word your question and also provide actual code you have tried that does not work.
JonathanSterling 9-Jun-15 1:52am    
AngusOAP take a look at this article about creating excel's chart elements with VB.NET.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900