Solved

Formatting plot area for named excel charts using VBA

Posted on 2014-02-23
3
1,109 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 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