+ Reply to Thread
Results 1 to 3 of 3

Excel 2007 Chart VBA tips

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-03-2007
    Location
    Auckland, New Zealand
    MS-Off Ver
    2007
    Posts
    137

    Excel 2007 Chart VBA tips

    Hi,
    More often than not the various solutions posted for manipulating charts in Excel 2007 with VBA are issue specific, which may be adapted by other users to suit their needs.
    This is great, but often means trolling a large number of threads and posts before you find something you can use/adapt.

    Given that chart methods are very difficult to track down and the VBA chart object model in XL07 isn't terribly helpful, and to possibly help users with basic manipulation I've started this thread in the hope that others will contribute snippets of code for making changes to charts in XL07 using VBA.

    To start the code below can be used to make the chart border invisible for every chart in the active sheet.

    Sub Remove_Chart_Border()
    ' Use this to make the border on each chart in the active sheet invisible
    '
    ' Get the number of charts in the active sheet
    NumberOfChartsInActiveSheet = ActiveSheet.ChartObjects.Count
    '
    'Create a For/Next loop to cycle through each chart in the active sheet
    For ChartLoop = 1 To NumberOfChartsInActiveSheet
    '
    'activate the next chart in the loop
    ActiveSheet.ChartObjects(ChartLoop).Activate
    ' Do something with the active chart
    With ActiveChart
    ' In this case the "do something" is set the line of the border of the Chart Area to invisible (visible = false)
        .ChartArea.Format.Line.Visible = msoFalse
    End With
    '
    'move on to next chart in the loop
    Next
    '
    '
    End Sub
    My knowledge of manipulating charts with VBA is very limited, so I'm hoping other (likely more experienced) forum members will see this as a good idea and tag on.

  2. #2
    Forum Contributor
    Join Date
    04-03-2007
    Location
    Auckland, New Zealand
    MS-Off Ver
    2007
    Posts
    137

    Re: Excel 2007 Chart VBA tips

    Change Position of Legend

    The below can be used to activate each chart in the activesheet and reposition the legend based on values entered by users.

    Sub Change_Position_Of_Legend()
    '
    ' For each chart in the active sheet, activate chart and change position of Legend
    '
    Dim NumberOfChartsInActiveSheet As Integer ' create a number variable to reflect the number of charts in the active sheet
    Dim LegendDistanceFromTop As Double ' create a number variable to reflect the desired distance of the Legend from the TOP of the active chart
    Dim LegendDistanceFromleft As Double ' create a number variable to reflect the desired distance of the Legend from the LEFT of the active chart
    '
    ' Get the number of charts in the active sheet
    NumberOfChartsInActiveSheet = ActiveSheet.ChartObjects.Count
    '
    ' Ask the user to specify value for the "LegendDistanceFromTop" number
    LegendDistanceFromTop = CDbl(InputBox(Prompt:="Enter the 'Distance From Top' for the chart Legends", Title:="Chart Legend Attributes"))
    ' use the below to use a cell value to specify the distance from Top
    ' LegendDistanceFromTop = CDbl(range("B2").value)' where cell B2 contains a whole number
    '
    ' Ask the user to specify value for the "LegendDistanceFromleft" number
    LegendDistanceFromleft = CDbl(InputBox(Prompt:="Enter the 'Distance From Left' for the chart Legends", Title:="Chart Legend Attributes"))
    ' use the below to use a cell value to specify the distance from Left
    ' LegendDistanceFromLeft = CDbl(range("B3").value)' where cell B3 contains a whole number
    '
    ' Turn off screen updating to help the code run faster
    Application.ScreenUpdating = False
    '
    ' Create a For/Next loop to cycle through each chart in the active sheet
    For ChartLoop = 1 To NumberOfChartsInActiveSheet
    '
    ' Activate the next chart in the loop
    ActiveSheet.ChartObjects(ChartLoop).Activate
    ' Now do something with the active chart
    With ActiveChart
    ' In this case the "do something" is move the chart legend by changing the distance from the TOP and LEFT of the active chart
        .Legend.Top = LegendDistanceFromTop
        .Legend.Left = LegendDistanceFromleft
    
    End With
    '
    'move on to next chart in the loop
    Next
    '
    '
    Application.ScreenUpdating = True
    '
    ' Simple message to let users know that the repositioning is finished
    MsgBox ("The Legend on each chart in the active sheet has been repositioned")
    End Sub

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Excel 2007 Chart VBA tips

    Here's a useful source on the graphs OM
    http://msdn.microsoft.com/en-gb/library/bb259482.aspx

    You might want to post links to those forum threads that contain useful code,
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1