cellMatrix.net
Spreadsheet Modeling and Related Topics

About

Formulas, Charts, and Models Created with Microsoft Excel.
More . . .

Statistics

  • Total Entries - 102
  • Current Viewers - 10

Categories

Recent Comments

Syndicate

Validate

Align Charts to Worksheet Grid Post 1

When a series of embedded charts are printed on a page and assuming all of the charts are the same height and width, for a more professional presentation consider aligning the charts horizontally and vertically with an even amount of spacing between. Aligning charts via worksheet cell ranges can help to do so. The code below automates that process and is useful for aligning many embedded charts at one time. There are six inputs:

  • Rng1 - the starting point for the chart at the top left side of the worksheet.
  • ChartsAcross - the number of charts that you want across the worksheet.
  • ColumnsAcross - increments the number of columns between Rng1.
  • RowsDown - increments the number of rows between Rng1.
  • Cht.Height - the height for each chart on the worksheet.
  • Cht.Width - the width for each chart on the worksheet.
Sub AlignCharts()

    Dim Cht As ChartObject
    Dim ChartsAcross As Integer
    Dim ColumnsAcross As Integer
    Dim RowsDown As Integer
    Dim Rng1 As Range
    Dim Rng2 As Range

    Set Rng1 = Range("B2")
    ChartsAcross = 2
    ColumnsAcross = 4
    RowsDown = 10
       
    For Each Cht In ActiveSheet.ChartObjects
        Cht.Height = 94.5
        Cht.Width = 144
    Next Cht

    Set Rng2 = Rng1

    For Each Cht In ActiveSheet.ChartObjects
        Cnt = Cnt + 1
    Next Cht

    For A = 1 To Cnt

        Set Shp = ActiveSheet.Shapes(A)
        Shp.Top = Rng1.Top
        Shp.Left = Rng1.Left

        If Application.WorksheetFunction.Round(A / ChartsAcross, 0) _
        - (A / ChartsAcross) = 0 Then
            Set Rng1 = Rng2.Offset(RowsDown, 0)
            Set Rng2 = Rng1
        Else
            Set Rng1 = Rng1.Offset(0, ColumnsAcross)
        End If

    Next A

End Sub

Given the assumptions above, the code works by first counting the number of charts on the worksheet. The first chart is placed at the top / left of cell B2. An If-Then-Else statement is used to place the following charts to the right of the chart before or to increment to the row range. The statement works by comparing the rounded (integer) value of the chart count divided by the total number of charts against the real number of the chart count divided by the total number of charts. If the divisor has a remainder, the chart is placed to the right of the chart before it. If the divisor does not have a remainder, the range increments to the next row.

The end result given the assumptions above returns the following:

image