Solved

Creating Excel chart using Visual Basic

Posted on 2000-05-04
2
803 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

728 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