Solved

Creating Excel chart using Visual Basic

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to open text file 11 136
Graph API & MS Apps 1 53
Developing a front end to SPLUNK 1 54
Remove Exif Orientation before adding image to db 9 38
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

740 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