Solved

Resize charts based on active chart

Posted on 2014-03-08
2
367 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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 will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

777 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