Solved

Creating Excel chart using Visual Basic

Posted on 2000-05-04
2
808 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month5 days, 18 hours left to enroll

627 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