Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 16th, 2010, 08:34 PM
Registered User
 
Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Formatting Excel charts with VBA

I cannot seem to get my VBA code to format a chart legend border, or even a chart area border. In fact the chart and the legend as well do not even appear to have a "borders" property. Has anyone run into this? When I try to record a macro to see how it's done there's no code in the recorded macro!
 
Old March 18th, 2010, 06:39 PM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Well, Not sure what exactly you're looking for but try this simple code sample with 5 columns and 7 rows (including heading row):
Code:
Public Sub MakeChart()

'Creates a table on current data 7 rows with heading and 5 columns
  Dim wsTarget As Worksheet, chToMake As ChartObject
  Set wsTarget = ActiveWorkbook.ActiveSheet
  Set chToMake = wsTarget.ChartObjects.Add(20, 100, 200, 200)
  With chToMake.Chart
    .ChartType = xlLine
    .HasTitle = True
    .ChartTitle.Caption = "Chart Title"
    .SetSourceData Source:=wsTarget.Range("A1:E7")
    .HasLegend = True
    With .Legend.Border
      .LineStyle = xlContinuous
      .Weight = xlMedium
    End With
    With .PlotArea.Border
      .LineStyle = xlContinuous
      .Weight = xlThin
    End With
  End With

End Sub
Hopefully this helps point in the right direction.
 
Old March 24th, 2010, 06:40 PM
Registered User
 
Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Smile thank you Allenm

Yes, it does help, but it does not completely solve my problem. "Borders" does not come up as an option on my IntelliSense when I type "Legend." This makes it difficult to ferret out the parent/child relationships between the various objects, properties, and methods.

A larger problem is to make one chart look like a similar one. I found a way to do it by copying the first chart and then using the PasteSpecial Format:=2 method on the second chart. But that's kind of clunky. It seems that if I had a pre-knowledge of all of the properties of the first chart (including those of all of it's objects), I could just set the second chart's properties to the same values. But the trouble is I haven't found a way to exhaustively debug.Print all of the properties of a chart exhaustively.

You can't write code like this:

For Each Property In ActiveChart
debug.Print Property
Next Property

All of the various chart objects like the legend, the chart area, the plot area, etc. have their own properties. Many are read only (cannot be set with VBA code).

So how can one discover all of the properties of any chart, including those of all of it's attendant objects? And once I do manage to discover them, how do I create a new chart with different data that looks very much like the first chart? Anybody?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Charts to Word Sanjo VB How-To 1 March 31st, 2007 12:07 AM
Generating Charts with VBA davidimurray Excel VBA 5 September 21st, 2006 08:04 AM
Charts by VBA jaymur BOOK: Expert One-on-One Access Application Development 7 June 8th, 2006 12:59 AM
Excel Charts in Word migalley Excel VBA 4 March 5th, 2004 02:47 PM
Formatting Excel through VBA John Anthony Access VBA 0 July 31st, 2003 05:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.