Solved

Formatting plot area for named excel charts using VBA

Posted on 2014-02-23
3
1,079 Views
Last Modified: 2014-02-23
Dear Experts:

Below code formats the plot area for the selected chart.

I would like to run this code not only on the active chart but on all the charts of the active worksheet named as follows "MyChart##", ## stands for '0-9', i.e., e.g. MyChart01, MyChart02, MyChart04, MyChart10, MyChart23, MyChart99 etc.

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

Regards, Andreas

Sub FormatPlotArea_ActiveChart()
'Formatting the plot area for the selected chart
With ActiveChart
   .PlotArea.Left = (.ChartArea.Width - .PlotArea.Width) / 2
   .PlotArea.Top = Application.CentimetersToPoints(1.8)
End With
End Sub

Open in new window

0
Comment
Question by:AndreasHermle
  • 2
3 Comments
 
LVL 81

Assisted Solution

by:byundt
byundt earned 500 total points
ID: 39881020
Try the code like this:
Sub FormatPlotArea_ActiveChart()
'Formatting the plot area for all charts on sheet named MyChart##
Dim cht As ChartObject
For Each cht In ActiveSheet.ChartObjects
    If UCase(Left(cht.Name, 7)) = "MYCHART" And Len(cht.Name) = 9 Then
        If IsNumeric(Right(cht.Name, 2)) Then
            With cht.Chart
                .PlotArea.Left = (.ChartArea.Width - .PlotArea.Width) / 2
                .PlotArea.Top = Application.CentimetersToPoints(1.8)
            End With
        End If
    End If
Next
End Sub

Open in new window

0
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39881051
I can eliminate some of the If tests using the Like operator:
Sub FormatPlotArea_ActiveChart()
'Formatting the plot area for all charts on sheet named MyChart##
Dim cht As ChartObject
For Each cht In ActiveSheet.ChartObjects
    If UCase(cht.Name) Like "MYCHART##" Then
        With cht.Chart
            .PlotArea.Left = (.ChartArea.Width - .PlotArea.Width) / 2
            .PlotArea.Top = Application.CentimetersToPoints(1.8)
        End With
    End If
Next
End Sub

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
ID: 39881082
Great, this did the trick. Thank you very much for your great and professional support.

Regards, Andreas
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

821 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