|
|
|
|
|
|
Excel
|
align (resize and move) charts in a worksheet
|
|
Ease of Use
|
Easy
|
Version tested with
|
2000
|
Submitted by:
|
MWE
|
Description:
|
xlAlignCharts resizes and moves charts on a sheet such that charts are equal size and aligned.
|
Discussion:
|
Resizing and aligning charts in Excel can be tedious. xlAlignCharts will automate this task. Based on the size and location of the active chart, the procedure resizes all charts to be the same size as the active chart and moves all charts except the active chart into a user defined # rows x # cols grid. The location of the active chart is used for the upper left corner of the chart grid.
|
Code:
|
instructions for use
|
Option Explicit
Sub xlAlignCharts()
Dim Ans As VbMsgBoxResult
Dim ChartNums() As Long
Dim HorzInc As Long
Dim I As Long
Dim J As Long
Dim Left As Long
Dim NumCharts As Long
Dim NumCols As Long
Dim NumRows As Long
Dim StartChart As String
Dim StartHeight As Long
Dim StartLeft As Long
Dim StartNum As Long
Dim StartTop As Long
Dim StartWidth As Long
Dim Top As Long
Dim VertInc As Long
Dim xlShape As Shape
NumCharts = ActiveSheet.ChartObjects.Count
If NumCharts < 1 Then
MsgBox "no charts on this sheet.", vbCritical + vbOKOnly
Exit Sub
End If
If NumCharts = 1 Then
MsgBox "only 1 chart on this sheet.", vbCritical + vbOKOnly
Exit Sub
End If
ReDim ChartNums(NumCharts)
On Error GoTo Error_NoActiveChart
SetStartChart:
StartChart = Trim(Right(ActiveChart.Name, Len(ActiveChart.Name) - Len(ActiveSheet.Name)))
StartHeight = ActiveSheet.Shapes(StartChart).Height
StartLeft = ActiveSheet.Shapes(StartChart).Left
StartTop = ActiveSheet.Shapes(StartChart).Top
StartWidth = ActiveSheet.Shapes(StartChart).Width
HorzInc = StartWidth / 20
VertInc = HorzInc
StartNum = 0
For I = 1 To NumCharts
If ActiveSheet.ChartObjects(I).Name = StartChart Then
StartNum = I
Exit For
End If
Next I
J = StartNum - 1
For I = 1 To NumCharts
J = J + 1
If J > NumCharts Then J = 1
ChartNums(I) = J
Next I
On Error GoTo Error_BadInput
GetRowCol:
NumRows = InputBox("# of chart rows?")
NumCols = InputBox("# of chart cols?")
If NumRows * NumCols < NumCharts Then
Ans = MsgBox("based on input only " & NumRows * NumCols & " charts will" & vbCrLf & _
"resized and moved. OK", vbQuestion + vbYesNoCancel)
If Ans = vbCancel Then Exit Sub
If Ans = vbNo Then GoTo GetRowCol
End If
Top = StartTop
Left = StartLeft + StartWidth + HorzInc
NumCharts = 0
For I = 1 To NumRows
For J = 1 To NumCols
NumCharts = NumCharts + 1
If NumCharts > ActiveSheet.ChartObjects.Count Then Exit Sub
Set xlShape = ActiveSheet.Shapes(ActiveSheet.ChartObjects(ChartNums(NumCharts)).Name)
If xlShape.Name <> StartChart Then
xlShape.Top = Top
xlShape.Left = Left
xlShape.Height = StartHeight
xlShape.Width = StartWidth
Left = Left + StartWidth + HorzInc
End If
Next J
Top = Top + StartHeight + VertInc
Left = StartLeft
Next I
Exit Sub
Error_NoActiveChart:
MsgBox "No active chart. Select a chart to be in upper left " & vbCrLf & _
"corner of 'chart grid' and rerun procedure.", vbCritical + vbOKOnly
Exit Sub
Error_BadInput:
End Sub
|
How to use:
|
- Copy the above code.
- Open any workbook.
- Press Alt + F11 to open the Visual Basic Editor (VBE).
- In the left side window, select the target spreadsheet [it will likely be called VBAProject(name.xls) where name is the name of the spreadsheet]
- Select an existing code module for the target worksheet; or from the Insert Menu, choose Insert | Module.
- Paste the code into the right-hand code window.
- Close the VBE, save the file if desired.
- See ?Test The Code? below
|
Test the code:
|
- In the attached example, there is a single worksheet with 5 charts.
- Open the example spreadsheet.
- Select one chart and make it the size you want. Then move it to the appropriate location (will be the upper left of the chart grid)
- Click on the yellow button or Go to Tools | Macro | Macros (or Alt+F8) and double-click on the procedure xlAlignChart
- Answer the two questions about # rows (of charts) and # cols (of charts)
|
Sample File:
|
AlignCharts.zip 16.01KB
|
Approved by mdmackillop
|
This entry has been viewed 138 times.
|
|