PC Review


Reply
Thread Tools Rate Thread

Create several charts in one go using VBA

 
 
andreashermle
Guest
Posts: n/a
 
      5th Aug 2010
Dear Experts:

below code creates a simple bar chart from a list (defined as table1)
on sheet 2.
I got several lists on sheet 1, all of them defined as tables, that is
Table2, Table3, and Table4

Is it possible to loop thru all these lists (defined as tables) in
sheet 2 and have the same bar chart created from all these lists
(defined as table1, table2, table3 and table4) in one go using VBA.

Help is very much appreciated. Thank you very much in advance.

Regards, Andreas

All of the charts should have the same Diagramm Title ($B$6)

Sub AddBarChart()
Dim myChtObj As ChartObject

Set myChtObj = ActiveSheet.ChartObjects.Add _
(Left:=100, Width:=400, Top:=75, Height:=200)

With myChtObj.Chart
.SetSourceData Source:=Sheets("Sheet2").Range("Table1")
.ChartType = xlBarClustered
.HasLegend = False
.Axes(xlCategory).MajorTickMark = xlNone
.SeriesCollection(1).ApplyDataLabels
.SeriesCollection(1).DataLabels.Position =
xlLabelPositionInsideEnd
With .Axes(xlValue)
.MinimumScale = 0
.MaximumScale = 10
.Delete
.MajorGridlines.Delete
End With

End With

With myChtObj.Chart
.SetElement (msoElementChartTitleAboveChart)
.ChartTitle.Text = "=Sheet2!$B$6"
End With


With myChtObj.Chart.Parent
.Top = Range("A7").Top
.Left = Range("E7").Left
.Name = "Chart1"
End With
 
Reply With Quote
 
 
 
 
Don Guillett Excel MVP
Guest
Posts: n/a
 
      5th Aug 2010
On Aug 5, 11:06*am, andreashermle <andreas.her...@gmx.de> wrote:
> Dear Experts:
>
> below code creates a simple bar chart from a list (defined as table1)
> on sheet 2.
> I got several lists on sheet 1, all of them defined as tables, that is
> Table2, Table3, and Table4
>
> Is it possible to loop thru all these lists (defined as tables) in
> sheet 2 and have the same bar chart created from all these lists
> (defined as table1, table2, table3 and table4) in one go using VBA.
>
> Help is very much appreciated. Thank you very much in advance.
>
> Regards, Andreas
>
> All of the charts should have the same Diagramm Title ($B$6)
>
> Sub AddBarChart()
> Dim myChtObj As ChartObject
>
> * * Set myChtObj = ActiveSheet.ChartObjects.Add _
> * * * * (Left:=100, Width:=400, Top:=75, Height:=200)
>
> * * * * With myChtObj.Chart
> * * * * * * .SetSourceData Source:=Sheets("Sheet2").Range("Table1")
> * * * * * * .ChartType = xlBarClustered
> * * * * * * .HasLegend = False
> * * * * * * .Axes(xlCategory).MajorTickMark = xlNone
> * * * * * * .SeriesCollection(1).ApplyDataLabels
> * * * * * * .SeriesCollection(1).DataLabels.Position =
> xlLabelPositionInsideEnd
> * * * * * * * * With .Axes(xlValue)
> * * * * * * * * * * *.MinimumScale = 0
> * * * * * * * * * * *.MaximumScale = 10
> * * * * * * * * * * *.Delete
> * * * * * * * * * * *.MajorGridlines.Delete
> * * * * * * * * End With
>
> * * * * End With
>
> * * * * With myChtObj.Chart
> * * * * * * .SetElement (msoElementChartTitleAboveChart)
> * * * * * * .ChartTitle.Text = "=Sheet2!$B$6"
> * * * * End With
>
> * * * * With myChtObj.Chart.Parent
> * * * * * * .Top = Range("A7").Top
> * * * * * * .Left = Range("E7").Left
> * * * * * * .Name = "Chart1"
> * * * * End With


"If desired, send your file to dguillett @gmail.com I will only look
if:
1. You send a copy of this message on an inserted sheet
2. You give me the newsgroup and the subject line
3. You send a clear explanation of what you want
4. You send before/after examples and expected results."
 
Reply With Quote
 
andreashermle
Guest
Posts: n/a
 
      5th Aug 2010
On Aug 5, 8:51*pm, Don Guillett Excel MVP <dguille...@austin.rr.com>
wrote:
> On Aug 5, 11:06*am, andreashermle <andreas.her...@gmx.de> wrote:
>
>
>
>
>
> > Dear Experts:

>
> > below code creates a simple bar chart from a list (defined as table1)
> > on sheet 2.
> > I got several lists on sheet 1, all of them defined as tables, that is
> > Table2, Table3, and Table4

>
> > Is it possible to loop thru all these lists (defined as tables) in
> > sheet 2 and have the same bar chart created from all these lists
> > (defined as table1, table2, table3 and table4) in one go using VBA.

>
> > Help is very much appreciated. Thank you very much in advance.

>
> > Regards, Andreas

>
> > All of the charts should have the same Diagramm Title ($B$6)

>
> > Sub AddBarChart()
> > Dim myChtObj As ChartObject

>
> > * * Set myChtObj = ActiveSheet.ChartObjects.Add _
> > * * * * (Left:=100, Width:=400, Top:=75, Height:=200)

>
> > * * * * With myChtObj.Chart
> > * * * * * * .SetSourceData Source:=Sheets("Sheet2").Range("Table1")
> > * * * * * * .ChartType = xlBarClustered
> > * * * * * * .HasLegend = False
> > * * * * * * .Axes(xlCategory).MajorTickMark = xlNone
> > * * * * * * .SeriesCollection(1).ApplyDataLabels
> > * * * * * * .SeriesCollection(1).DataLabels.Position =
> > xlLabelPositionInsideEnd
> > * * * * * * * * With .Axes(xlValue)
> > * * * * * * * * * * *.MinimumScale = 0
> > * * * * * * * * * * *.MaximumScale = 10
> > * * * * * * * * * * *.Delete
> > * * * * * * * * * * *.MajorGridlines.Delete
> > * * * * * * * * End With

>
> > * * * * End With

>
> > * * * * With myChtObj.Chart
> > * * * * * * .SetElement (msoElementChartTitleAboveChart)
> > * * * * * * .ChartTitle.Text = "=Sheet2!$B$6"
> > * * * * End With

>
> > * * * * With myChtObj.Chart.Parent
> > * * * * * * .Top = Range("A7").Top
> > * * * * * * .Left = Range("E7").Left
> > * * * * * * .Name = "Chart1"
> > * * * * End With

>
> "If desired, send your file to dguillett *@gmail.com I will only look
> if:
> 1. You send a copy of this message on an inserted sheet
> 2. You give me the newsgroup and the subject line
> 3. You send a clear explanation of what you want
> 4. You send before/after examples and expected results."- Hide quoted text -
>
> - Show quoted text -


Hi Don,

what a service! Will do the requested shorty.

Thank you. Regards, Andreas
 
Reply With Quote
 
Jon Peltier
Guest
Posts: n/a
 
      10th Aug 2010
Pseudocode:

For i=1 to 10
Set myChtObj = ... (choose position so no overlapping charts)
myChtObj.Chart.SetSourceData Source:= (appropriate i'th range)
Next

- Jon
-------
Jon Peltier
Peltier Technical Services, Inc.
http://peltiertech.com/


On 8/5/2010 12:06 PM, andreashermle wrote:
> Dear Experts:
>
> below code creates a simple bar chart from a list (defined as table1)
> on sheet 2.
> I got several lists on sheet 1, all of them defined as tables, that is
> Table2, Table3, and Table4
>
> Is it possible to loop thru all these lists (defined as tables) in
> sheet 2 and have the same bar chart created from all these lists
> (defined as table1, table2, table3 and table4) in one go using VBA.
>
> Help is very much appreciated. Thank you very much in advance.
>
> Regards, Andreas
>
> All of the charts should have the same Diagramm Title ($B$6)
>
> Sub AddBarChart()
> Dim myChtObj As ChartObject
>
> Set myChtObj = ActiveSheet.ChartObjects.Add _
> (Left:=100, Width:=400, Top:=75, Height:=200)
>
> With myChtObj.Chart
> .SetSourceData Source:=Sheets("Sheet2").Range("Table1")
> .ChartType = xlBarClustered
> .HasLegend = False
> .Axes(xlCategory).MajorTickMark = xlNone
> .SeriesCollection(1).ApplyDataLabels
> .SeriesCollection(1).DataLabels.Position =
> xlLabelPositionInsideEnd
> With .Axes(xlValue)
> .MinimumScale = 0
> .MaximumScale = 10
> .Delete
> .MajorGridlines.Delete
> End With
>
> End With
>
> With myChtObj.Chart
> .SetElement (msoElementChartTitleAboveChart)
> .ChartTitle.Text = "=Sheet2!$B$6"
> End With
>
>
> With myChtObj.Chart.Parent
> .Top = Range("A7").Top
> .Left = Range("E7").Left
> .Name = "Chart1"
> End With

 
Reply With Quote
 
andreashermle
Guest
Posts: n/a
 
      31st Aug 2010
On 10 Aug., 18:23, Jon Peltier <jonpeltier.nos...@gmail.com> wrote:
> Pseudocode:
>
> For i=1 to 10
> * *Set myChtObj = ... (choose position so no overlapping charts)
> * *myChtObj.Chart.SetSourceData Source:= (appropriate i'th range)
> Next
>
> - Jon
> -------
> Jon Peltier
> Peltier Technical Services, Inc.http://peltiertech.com/
>
> On 8/5/2010 12:06 PM, andreashermle wrote:
>
>
>
> > Dear Experts:

>
> > below code creates a simple bar chart from a list (defined as table1)
> > on sheet 2.
> > I got several lists on sheet 1, all of them defined as tables, that is
> > Table2, Table3, and Table4

>
> > Is it possible to loop thru all these lists (defined as tables) in
> > sheet 2 and have the same bar chart created from all these lists
> > (defined as table1, table2, table3 and table4) in one go using VBA.

>
> > Help is very much appreciated. Thank you very much in advance.

>
> > Regards, Andreas

>
> > All of the charts should have the same Diagramm Title ($B$6)

>
> > Sub AddBarChart()
> > Dim myChtObj As ChartObject

>
> > * * *Set myChtObj = ActiveSheet.ChartObjects.Add _
> > * * * * *(Left:=100, Width:=400, Top:=75, Height:=200)

>
> > * * * * *With myChtObj.Chart
> > * * * * * * *.SetSourceData Source:=Sheets("Sheet2").Range("Table1")
> > * * * * * * *.ChartType = xlBarClustered
> > * * * * * * *.HasLegend = False
> > * * * * * * *.Axes(xlCategory).MajorTickMark = xlNone
> > * * * * * * *.SeriesCollection(1).ApplyDataLabels
> > * * * * * * *.SeriesCollection(1).DataLabels.Position =
> > xlLabelPositionInsideEnd
> > * * * * * * * * *With .Axes(xlValue)
> > * * * * * * * * * * * .MinimumScale = 0
> > * * * * * * * * * * * .MaximumScale = 10
> > * * * * * * * * * * * .Delete
> > * * * * * * * * * * * .MajorGridlines.Delete
> > * * * * * * * * *End With

>
> > * * * * *End With

>
> > * * * * *With myChtObj.Chart
> > * * * * * * *.SetElement (msoElementChartTitleAboveChart)
> > * * * * * * *.ChartTitle.Text = "=Sheet2!$B$6"
> > * * * * *End With

>
> > * * * * *With myChtObj.Chart.Parent
> > * * * * * * *.Top = Range("A7").Top
> > * * * * * * *.Left = Range("E7").Left
> > * * * * * * *.Name = "Chart1"
> > * * * * *End With- Zitierten Text ausblenden -

>
> - Zitierten Text anzeigen -


Hi Jon,

thank you very much for your great help. I could incorporate your code
snippets. It works.

Thank you.

Regards, Andreas
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to click on charts & unable to create any new charts Excel Snapclick Microsoft Excel Charting 4 1st Feb 2009 07:59 PM
how to create 0 to 0 value charts =?Utf-8?B?dmFtc2k=?= Microsoft Excel Charting 1 24th Sep 2007 12:37 PM
how do i create 4 pie charts =?Utf-8?B?TWlrZSBLaW0=?= Microsoft Excel Charting 1 21st Feb 2006 10:22 AM
Trying to create some charts - how do I do this? Mctabish Microsoft Excel Discussion 0 2nd Jan 2006 09:36 AM
Best way to create 27 charts with vba? Gunnar Johansson Microsoft Excel Programming 3 22nd Oct 2004 02:15 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:42 AM.