Solved

Formatting plot area for named excel charts using VBA

Posted on 2014-02-23
3
1,050 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now