PTS Blog

Main menu:

PTS Utilities

Commercial Utilities developed by Peltier Technical Services

  Waterfall Chart

Excel Books

Books that I own and use while developing in Excel

Goods and Services

Excel or charting related products and services which I use or feel are worthwhile additions

Subscribe

Site search


Recent Posts

Recently Commented

March 2008
S M T W T F S
« Feb   Apr »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Archive


 

Categories


 

Creating Charts in a Grid

by Jon Peltier
Peltier Technical Services, Inc., Copyright © 2008. All rights reserved.

A few days ago, in Arranging Charts in a Grid, I posted a VBA procedure that arranged all the chart objects on a worksheet into a regular grid. This is pretty nice, but it arranges the charts left to right, then top to bottom, based on the Z stacking order of the charts. If you make the charts in a different order, or change their stacking order, you will get a different positioning of the various charts within the grid.

Often you have your data in a table, and you want to plot them all and arrange them in a nice array. The enhanced VBA procedure in this post kills two birds: creating AND arranging the charts.

This procedure needs a worksheet with the following layout.

The yellow range is named "Settings" (B2:K3 in this example, but it can be any 2-row by 10 column range with the items in this order), and contains many of the adjustable constants from the previous procedure. Placing them in the worksheet makes it easier to adjust them, since you do not need to hunt through the code to change a parameter. The headers in B2:K2 are hard to read in the screen shot, so here they are again, with descriptions:

RowsHigh - Height of each chart, in worksheet rows
ColsWide - Width of each chart, in worksheet columns
FirstRow - Topmost worksheet row covered by charts
FirstCol - Leftmost worksheet column covered by charts
SkipRows - Worksheet rows to skip between rows of charts
SkipCols - Worksheet columns to skip between columns of charts
NAcross - Number of charts per row of charts
FontSize - Font size used in the charts
RowHeight - Height of worksheet rows, in points
ColWidth - Width of worksheet columns, in characters of default font

The green cell is named "DataAll" (B5 in this example). and is the top left cell of a two-row range with the category labels (C5:H5), the series name (B6), and values (C6:H6) for a series which will be plotted on each chart in the grid. The purple cell (B8) is named "DataEach", and is the top left cell of a N+1 row range with category labels (C8:H8), N series names (B9 downward), and N sets of Y values (C9:H9 and downward), for N series that will be plotted in N charts. In this case there are 12 rows of Y values, and they will appear in 12 charts. The DataAll and DataEach ranges don't need to be laid out nicely with respect to each other, but it makes it easier to keep track. They do need to be isolated, with empty rows and columns separating them from other data.

The procedure is shown below. The setup worksheet shown above should be activated before running the procedure. A new worksheet is inserted and the charts arrayed on this new sheet. The procedure steps down the DataEach range

Visual Basic:
  1. Sub MakeGridOfCharts()<br />
  2.  ' Jon Peltier 3/19/2008</p>
  3.  
  4. <p>' inputs<br />
  5.  Dim vSettings As Variant<br />
  6.  Dim rDataAll As Range<br />
  7.  Dim rDataEach As Range</p>
  8.  
  9. <p>' chart size - adjust as desired<br />
  10.  Dim nRowsTall As Long<br />
  11.  Dim nColsWide As Long<br />
  12.  Dim nFontSize As Double</p>
  13.  
  14. <p>' chart layout - adjust as desired<br />
  15.  Dim nChartsPerRow As Long<br />
  16.  Dim nSkipRows As Long<br />
  17.  Dim nSkipCols As Long<br />
  18.  Dim nFirstRow As Long<br />
  19.  Dim nFirstCol As Long</p>
  20.  
  21. <p>Dim wsData As Worksheet<br />
  22.  Dim wsChart As Worksheet<br />
  23.  Dim iChart As Long<br />
  24.  Dim chtob As ChartObject<br />
  25.  Dim srs As Series<br />
  26.  Dim dWidth As Double<br />
  27.  Dim dHeight As Double<br />
  28.  Dim rData As Range<br />
  29.  Dim dFirstChartTop As Double<br />
  30.  Dim dFirstChartLeft As Double<br />
  31.  Dim dRowsBetweenChart As Double<br />
  32.  Dim dColsBetweenChart As Double<br />
  33.  Dim dRowHeight As Double<br />
  34.  Dim dColWidth As Double</p>
  35.  
  36. <p>Application.ScreenUpdating = False</p>
  37.  
  38. <p>' define worksheets<br />
  39.  Set wsData = ActiveSheet<br />
  40.  Set wsChart = Worksheets.Add(After:=wsData)</p>
  41.  
  42. <p>' define data inputs<br />
  43.  vSettings = wsData.Range("Settings").Value<br />
  44.  Set rDataAll = wsData.Range("DataAll").CurrentRegion<br />
  45.  Set rDataEach = wsData.Range("DataEach").CurrentRegion</p>
  46.  
  47. <p>' extract layout parameters<br />
  48.  nRowsTall = vSettings(2, 1)<br />
  49.  nColsWide = vSettings(2, 2)<br />
  50.  nFirstRow = vSettings(2, 3)<br />
  51.  nFirstCol = vSettings(2, 4)<br />
  52.  nSkipRows = vSettings(2, 5)<br />
  53.  nSkipCols = vSettings(2, 6)<br />
  54.  nChartsPerRow = vSettings(2, 7)<br />
  55.  nFontSize = vSettings(2, 8)<br />
  56.  dRowHeight = vSettings(2, 9)<br />
  57.  dColWidth = vSettings(2, 10)</p>
  58.  
  59. <p>If dRowHeight&gt; 0 Then<br />
  60.  wsChart.Rows.RowHeight = dRowHeight<br />
  61.  End If<br />
  62.  If dColWidth&gt; 0 Then<br />
  63.  wsChart.Columns.ColumnWidth = dColWidth<br />
  64.  End If</p>
  65.  
  66. <p>With wsChart.Cells(1, 1)<br />
  67.  dWidth = nColsWide * .Width<br />
  68.  dHeight = nRowsTall * .Height<br />
  69.  dFirstChartLeft = (nFirstCol - 1) * .Width<br />
  70.  dFirstChartTop = (nFirstRow - 1) * .Height<br />
  71.  dRowsBetweenChart = nSkipRows * .Height<br />
  72.  dColsBetweenChart = nSkipCols * .Width<br />
  73.  End With</p>
  74.  
  75. <p>For iChart = 1 To rDataEach.Rows.Count - 1</p>
  76.  
  77. <p>' create the chart<br />
  78.  Set chtob = wsChart.ChartObjects.Add( _<br />
  79.  ((iChart - 1) Mod nChartsPerRow) * (dWidth + dColsBetweenChart) + dFirstChartLeft, _<br />
  80.  Int((iChart - 1) / nChartsPerRow) * (dHeight + dRowsBetweenChart) + dFirstChartTop, _<br />
  81.  dWidth, dHeight)</p>
  82.  
  83. <p>' add standard series (common to all charts)<br />
  84.  Set srs = chtob.Chart.SeriesCollection.NewSeries<br />
  85.  With srs<br />
  86.  .Name = rDataAll.Cells(2, 1)<br />
  87.  .Values = rDataAll.Cells(2, 2).Resize(, rDataAll.Columns.Count - 1)<br />
  88.  .XValues = rDataAll.Cells(1, 2).Resize(, rDataAll.Columns.Count - 1)<br />
  89.  .ChartType = xlLine<br />
  90.  .Border.Weight = xlMedium<br />
  91.  End With</p>
  92.  
  93. <p>' add variable series (unique to each chart)<br />
  94.  Set srs = chtob.Chart.SeriesCollection.NewSeries<br />
  95.  With srs<br />
  96.  .Name = rDataEach.Cells(iChart + 1, 1)<br />
  97.  .Values = rDataEach.Cells(iChart + 1, 2).Resize(, rDataEach.Columns.Count - 1)<br />
  98.  .XValues = rDataEach.Cells(1, 2).Resize(, rDataEach.Columns.Count - 1)<br />
  99.  .ChartType = xlLine<br />
  100.  .Border.Weight = xlMedium<br />
  101.  End With</p>
  102.  
  103. <p>' format chart<br />
  104.  With chtob.Chart<br />
  105.  .HasTitle = True<br />
  106.  .ChartTitle.Characters.Text = srs.Name<br />
  107.  With .ChartArea<br />
  108.  .AutoScaleFont = False<br />
  109.  .Font.Size = nFontSize<br />
  110.  .Border.ColorIndex = 2<br />
  111.  End With<br />
  112.  .HasLegend = False<br />
  113.  With .Axes(xlValue)<br />
  114.  .HasMajorGridlines = False<br />
  115.  .Border.ColorIndex = 48<br />
  116.  End With<br />
  117.  With .Axes(xlCategory)<br />
  118.  .Border.ColorIndex = 48<br />
  119.  With .TickLabels<br />
  120.  .Orientation = xlHorizontal<br />
  121.  .Offset = 0<br />
  122.  End With<br />
  123.  End With<br />
  124.  With .PlotArea<br />
  125.  .Border.ColorIndex = 48<br />
  126.  .Interior.ColorIndex = 2<br />
  127.  .Left = 0<br />
  128.  .Top = 0<br />
  129.  .Height = chtob.Chart.ChartArea.Height<br />
  130.  .Width = chtob.Chart.ChartArea.Width - 7<br />
  131.  End With<br />
  132.  With .ChartTitle<br />
  133.  .Font.Bold = True<br />
  134.  .Top = chtob.Chart.PlotArea.InsideTop<br />
  135.  .Left = chtob.Chart.PlotArea.InsideLeft + 3<br />
  136.  End With<br />
  137.  End With</p>
  138.  
  139. <p>Next</p>
  140.  
  141. <p>Application.ScreenUpdating = True</p>
  142.  
  143. <p>End Sub

By adjusting the values in the yellow cells, you can change the output sheet remarkably. This is starting to look like a dashboard.

Share/Save/Bookmark

Write a comment





Create Excel dashboards quickly with Plug-N-Play reports.