Solved

Resize charts based on active chart

Posted on 2014-03-08
2
356 Views
Last Modified: 2014-03-08
Dear Experts:

below macro (it is really a fantastic one) ...
... resizes all charts on the active worksheet so that they match
the dimensions of the active chart.
... It also arranges the charts into a user specified
number of columns.

Could somebody help me tweak this code so that ...
... only charts named 'MyCharts_##' are worked on.  (The number sign ## stands for 01, 02, 03, ... 12, 13 etc.)
The number of columns into which the charts get arranged should be fixed to 3 columns.

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

Regards, Andreas

Sub SizeAndAlignCharts()
Dim W As Long, H As Long
Dim TopPosition As Long, LeftPosition As Long
Dim ChtObj As ChartObject
Dim i As Long, NumCols As Long
If ActiveChart Is Nothing Then
MsgBox "Select a chart to be used as the base for the sizing"
Exit Sub
End If
'Get Columns
On Error Resume Next
NumCols = InputBox("How many columns of charts?")
If Err.Number <> 0 Then Exit Sub
If NumCols < 1 Then Exit Sub
On Error GoTo 0
'Get size of active chart
W = ActiveChart.Parent.Width
H = ActiveChart.Parent.Height
'Change starting positions, if necessary
TopPosition = 100
LeftPosition = 20
For i = 1 To ActiveSheet.ChartObjects.Count
    With ActiveSheet.ChartObjects(i)
        .Width = W
        .Height = H
        .Left = LeftPosition + ((i - 1) Mod NumCols) * W
        .Top = TopPosition + Int((i - 1) / NumCols) * H
    End With
Next i
End Sub

Open in new window

0
Comment
Question by:AndreasHermle
2 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39914912
Andreas,
I revised the macro to only align charts in a grid if they were named like MyCharts_xx, but did not test it. If there is any problem with the code, could you please post a sample workbook so I can test it?
Sub SizeAndAlignCharts()
Dim W As Long, H As Long
Dim TopPosition As Long, LeftPosition As Long
Dim ChtObj As ChartObject
Dim i As Long, j As Long, NumCols As Long
If ActiveChart Is Nothing Then
MsgBox "Select a chart to be used as the base for the sizing"
Exit Sub
End If
'Get Columns
On Error Resume Next
NumCols = InputBox("How many columns of charts?")
If Err.Number <> 0 Then Exit Sub
If NumCols < 1 Then Exit Sub
On Error GoTo 0
'Get size of active chart
W = ActiveChart.Parent.Width
H = ActiveChart.Parent.Height
'Change starting positions, if necessary
TopPosition = 100
LeftPosition = 20
For i = 1 To ActiveSheet.ChartObjects.Count
    If ActiveSheet.ChartObjects(i).Name Like "MyCharts_##" Then
        j = j + 1
        With ActiveSheet.ChartObjects(i)
            .Width = W
            .Height = H
            .Left = LeftPosition + ((j - 1) Mod NumCols) * W
            .Top = TopPosition + Int((j - 1) / NumCols) * H
        End With
    End If
Next i
End Sub

Open in new window

Brad
0
 

Author Closing Comment

by:AndreasHermle
ID: 39915077
As always, this works like a charm. Thank you very much for your professional help.

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

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
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…

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

22 Experts available now in Live!

Get 1:1 Help Now