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


 

Quick VBA Routine: XY Chart with Axis Titles

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

Someone asked in the newsgroup how to create an XY chart in Excel that uses the label at the top of the columns of X and Y data for the axis titles. That's not built in, but it's not hard to do if you know a little VBA. I've taken an example from my web site, Interactive Chart Creation, and embellished it a bit.

The VBA procedure (code below) actually works a bit like my first recollection of Excel charting, which had two dialogs, one asking for the data range, the other asking for the range of cells to cover witth the chart. Since I always align my charts with the cell boundaries, this is nicer than the Chart Wizard in Excel versions up to 2003 and the Insert Chart command in Excel 2007.

The code uses a couple of input boxes to get the ranges from the user. I've added some smarts so it uses the selection as the default for the source data range in the first input box. If the selection is a single cell, the macro uses the cell's current region (the contiguous range including the cell).

Copy the code below (use the plain text view of the code for copying) into a regular module in the VB Editor, then from Excel's Tools menu, choose Macro, then Macros, or use the Alt+F8 shortcut key combination, then select and run the macro ChartWithAxisTitles.

Macro dialog

The first input box asks you to select the data range. Note that the active cell C9 has been expanded to its current region B2:D21 for the dialog's default data range.

Select Chart Data Dialog

The second input box asks you to select a region for the chart to cover.

Insert Chart Position Dialog

The result is an XY chart with default chart and series formatting. I've cleaned up the formatting slightly here, after the procedure inserted the chart, but obviously there's room for improvement, particularly with the spacing of the chart elements.

Automatic Chart with Axis Titles

My data range had three columns. The first is used for X values and the X axis title, the others are used for Y values for two series. The cell above the first Y data column is used for the Y axis title, and the cell above each Y data column is used for the corresponding series name.

Here is the minimally documented code:

Visual Basic:
  1. Sub ChartWithAxisTitles()
  2.   Dim objChart As ChartObject
  3.   Dim myChtRange As Range
  4.   Dim myDataRange As Range
  5.   Dim myInitialRange As Range
  6.   Dim sInitialRange As String
  7.  
  8.   ' bail out if we're not on a worksheet
  9.   If Not ActiveSheet Is Nothing Then
  10.     If TypeName(ActiveSheet) = "Worksheet" Then
  11.      
  12.       ' propose an initial data range
  13.       If TypeName(Selection) = "Range" Then
  14.         Set myInitialRange = Selection
  15.         If myInitialRange.Cells.Count = 1 Then
  16.           Set myInitialRange = myInitialRange.CurrentRegion
  17.           sInitialRange = myInitialRange.Address(True, True)
  18.         End If
  19.       End If
  20.  
  21.       With ActiveSheet
  22.  
  23.         ' ask user for range that contains data for chart
  24.         On Error Resume Next
  25.         Set myDataRange = Application.InputBox( _
  26.             prompt:="Select a range containing the chart data.", _
  27.             Title:="Select Chart Data", Default:=sInitialRange, Type:=8)
  28.         On Error GoTo 0
  29.         If myDataRange Is Nothing Then Exit Sub
  30.  
  31.         ' ask user for range chart should cover
  32.         On Error Resume Next
  33.         Set myChtRange = Application.InputBox( _
  34.             prompt:="Select a range where the chart should appear.", _
  35.             Title:="Select Chart Position", Type:=8)
  36.         On Error GoTo 0
  37.         If myChtRange Is Nothing Then Exit Sub
  38.  
  39.         ' cover chart range with chart
  40.         Set objChart = .ChartObjects.Add( _
  41.             Left:=myChtRange.Left, Top:=myChtRange.Top, _
  42.             Width:=myChtRange.Width, Height:=myChtRange.Height)
  43.  
  44.         ' Put all the right stuff in the chart
  45.         With objChart.Chart
  46.           .ChartArea.AutoScaleFont = False
  47.           .ChartType = xlXYScatterLines
  48.           .SetSourceData Source:=myDataRange, PlotBy:=xlColumns
  49.           .HasTitle = True
  50.           .ChartTitle.Characters.Text = "My Title"
  51.           .ChartTitle.Font.Bold = True
  52.           .ChartTitle.Font.Size = 10
  53.           With .Axes(xlCategory, xlPrimary)
  54.             .HasTitle = True
  55.             With .AxisTitle
  56.               .Characters.Text = myDataRange.Cells(1, 1)
  57.               .Font.Size = 8
  58.               .Font.Bold = True
  59.             End With
  60.           End With
  61.           With .Axes(xlValue, xlPrimary)
  62.             .HasTitle = True
  63.             With .AxisTitle
  64.               .Characters.Text = myDataRange.Cells(1, 2)
  65.               .Font.Size = 8
  66.               .Font.Bold = True
  67.             End With
  68.           End With
  69.         End With
  70.  
  71.       End With
  72.     End If
  73.   End If
  74. End Sub

Share/Save/Bookmark

Comments

Pingback from Quick VBA Routine: XY Chart with Axis Titles | Quick Work
Time: Thursday, March 6, 2008, 1:48 am

[...] admin wrote an interesting post today onHere’s a quick excerptSomeone asked in the newsgroup how to create an XY chart in Excel that uses the label at the top of the columns of X and Y data for the axis titles. That’s not built in, but it’s not hard to do if you know a little VBA. … [...]


Write a comment





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