Solved

Creating Excel chart using Visual Basic

Posted on 2000-05-04
2
782 Views
Last Modified: 2013-11-25
Hi all,
I am trying to create an Excel Chart using Visual Basic. My problem is that the chart type does not appear as i have set it. The way I am trying to do this is as follows:

xlApp.Charts.Add
xlApp.ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:="Line - Column"
<snip rest of code>

When the chart appears, it is always the default Chart (columns) not the built in type that i have specified above.

Does anyone have any ideas on how to fix this?

Thanks
0
Comment
Question by:jacy_m
2 Comments
 
LVL 3

Accepted Solution

by:
DrMaltz earned 100 total points
ID: 2779772
jacy_m, maybe it's the Type name that you are using.  I don't recognize "Line-column.  Here's  some code that I've used:

Dim oXL As Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oChartObjs As Excel.ChartObjects
Dim oChartObj As Excel.ChartObject
Dim oChart As Excel.Chart
      Dim i As Integer, j As Integer
      Dim sMsg As String
     
    ' Create a new instance of Excel and make it visible.
      Set oXL = CreateObject("Excel.Application")
      oXL.Visible = True
   
    ' Add a new workbook and set a reference to Sheet1.
      Set oBook = oXL.Workbooks.Add
      Set oSheet = oBook.Sheets(1)
     
     
    ' Demo standard Automation from out-of-process,
    ' this routine simply fills in values of cells.
      sMsg = "Fill Spread Sheet with Process Data"
      MsgBox sMsg, vbInformation Or vbMsgBoxSetForeground
     
      For i = 1 To 100
         For j = 1 To 10
            sMsg = "=" & Str(i) & " + " & Str(j)
            oSheet.Cells(i, j).Value = sMsg
         Next j
      Next i
      Set oChartObjs = oSheet.ChartObjects
      Set oChartObj = oChartObjs.Add(300, 200, 300, 350)
      Set oChart = oChartObj.Chart
     
      oChart.ChartType = xlLine
      oChart.SetSourceData Range("A1:j100"), xlColumns
           
      oSheet.SaveAs "d:\test.xls"
   
    ' Turn instance of Excel over to end user and release
    ' any outstanding object references.
      oXL.UserControl = True
      oXL.Quit
     
      Set oChartObjs = Nothing
      Set oChartObj = Nothing
      Set oChart = NOthing
      Set oSheet = Nothing
      Set oBook = Nothing
      Set oXL = Nothing
     
0
 
LVL 22

Expert Comment

by:ture
ID: 2779902
jacy_m,

Use an object variable that refers to the chart you created:

Dim xlChart as Excel.Chart
Set xlChart = xlApp.Charts.Add
xlChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:="Line - Column"

Better?

Ture Magnusson
Karlstad, Sweden
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using "ScreenUpdating" 6 55
What .NET website keeps me current? 9 57
Passing a Text Box name to a Sub 6 76
IF ELSE Statement in Excel Macro VBA 16 34
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now