Solved

Creating Excel chart using Visual Basic

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

743 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