Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Formatting plot area for named excel charts using VBA

Posted on 2014-02-23
3
Medium Priority
?
1,228 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 2000 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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

916 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