Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.charting
external usenet poster
 
Posts: 4
Default Macro to create a new chart

I've been using this code for a while to easily create a new chart.
However just recently has stopped working. It now created a "default"
excel chart as a new tab in the workbook. Any suggestions?

'************************************************* *******************
'Creates 2 data series horizontal bar chart
'************************************************* *******************

Dim myShtName As String
myShtName = ActiveSheet.Name
Charts.Add
ActiveChart.ChartType = xlBarClustered 'Creates Stacked bar
ActiveChart.SetSourceData Source:=Rng, PlotBy:=xlColumns
'References sheet and and cell range of data to generate chart
ActiveChart.Location Whe=xlLocationAsObject, Name:=myShtName
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
ActiveChart.HasLegend = True

'************************************************* *******************
'Specifies placement and height & width dimensions of chart
'************************************************* *******************

With ActiveChart.Parent
.Top = ActiveSheet.Range("F22:Q22").Top
.Left = ActiveSheet.Range("F22:Q52").Left
.Height = ActiveSheet.Range("F22:Q52").Height
.Width = ActiveSheet.Range("F22:Q22").Width
End With

  #2   Report Post  
Posted to microsoft.public.excel.charting
external usenet poster
 
Posts: 6,582
Default Macro to create a new chart

You'll save a lot of grief if you make the chart directly in the worksheet,
using the ChartObjects.Add method.

Dim wksMySheet As Worksheet

Dim chtMyChart As Chart

Dim dTop as Double

Dim dLeft as Double

Dim dHeight as Double

Dim dWidth as Double

Set wksMySheet = ActiveSheet

dTop = wksMySheet .Range("F22:Q22").Top

dLeft = wksMySheet .Range("F22:Q52").Left

dHeight = wksMySheet .Range("F22:Q52").Height

dWidth = wksMySheet .Range("F22:Q22").Width

Set chtMyChart = wksMySheet.ChartObjects.Add(dLeft, dTop, dWidth,
dHeight).Chart

With chtMyChart

.ChartType = xlBarClustered 'Creates Stacked bar

.SetSourceData Source:=Rng, PlotBy:=xlColumns

With .Axes(xlCategory)

.HasMajorGridlines = False

.HasMinorGridlines = False

End With

With .Axes(xlValue)

.HasMajorGridlines = False

.HasMinorGridlines = False

End With

.HasLegend = True

End With


More VBA charting tips:

http://peltiertech.com/Excel/ChartsH...kChartVBA.html

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"kimbobo" wrote in message
oups.com...
I've been using this code for a while to easily create a new chart.
However just recently has stopped working. It now created a "default"
excel chart as a new tab in the workbook. Any suggestions?

'************************************************* *******************
'Creates 2 data series horizontal bar chart
'************************************************* *******************

Dim myShtName As String
myShtName = ActiveSheet.Name
Charts.Add
ActiveChart.ChartType = xlBarClustered 'Creates Stacked bar
ActiveChart.SetSourceData Source:=Rng, PlotBy:=xlColumns
'References sheet and and cell range of data to generate chart
ActiveChart.Location Whe=xlLocationAsObject, Name:=myShtName
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
ActiveChart.HasLegend = True

'************************************************* *******************
'Specifies placement and height & width dimensions of chart
'************************************************* *******************

With ActiveChart.Parent
.Top = ActiveSheet.Range("F22:Q22").Top
.Left = ActiveSheet.Range("F22:Q52").Left
.Height = ActiveSheet.Range("F22:Q52").Height
.Width = ActiveSheet.Range("F22:Q22").Width
End With



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB Macro to Create a Chart [email protected] Excel Discussion (Misc queries) 0 August 15th 06 10:25 PM
Using a macro to create a macro in another workbook Gizmo63 Excel Worksheet Functions 2 May 15th 06 09:48 AM
Macro to change Chart Range when inserting a column Mark Charts and Charting in Excel 1 September 13th 05 01:12 PM
How do I create a "backwards" chart Sarah Charts and Charting in Excel 2 March 31st 05 05:08 AM
Macro to create macro Carl Bowman Excel Discussion (Misc queries) 2 February 6th 05 10:49 AM


All times are GMT +1. The time now is 02:25 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"