Solved

Formatting plot area for named excel charts using VBA

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

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 80

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
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…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

760 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

15 Experts available now in Live!

Get 1:1 Help Now