Peltier Tech Blog

Excel Chart Add-Ins | Training | Custom Solutions | Charts and Tutorials | PTS Blog

 

Main menu:

 
Peltier Tech
Chart Add-Ins

Peltier Tech Waterfall Chart Utility
Peltier Tech Cluster-Stack Chart Utility
Peltier Tech Box and Whisker Chart Utility
Peltier Tech Marimekko Chart Utility
Peltier Tech Dot Plot Utility
Peltier Tech Cascade Chart Utility

 
Excel Dashboards

Subscribe

Site search

Subscribe

Site search


Recent Posts

Recently Commented

Popular Posts

Archive


 

Categories


 

Privacy Policy

Creative Commons License
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

Build an Excel Add-In 5 – Tie the Code Together

by Jon Peltier
Tuesday, January 26th, 2010
Peltier Technical Services, Inc., Copyright © 2010.
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

This is the fifth in a series of posts that shows the steps involved in building an Excel add-in. In Build an Excel Add-In 1 – Basic Routine I showed a VBA procedure that creates a regular chart from an arbitrary rectangular range, including from a pivot table. In Build an Excel Add-In 2 – Enhanced Functionality I turned modularized this routine, so it could be called from any code that passed the appropriate settings into the function as arguments. In Build an Excel Add-In 3 – Auxiliary Modular Functions I included an additional modular function which cleans up the chart created in the previous article’s code. Because this function is widely useful, it will be made more general in a future article, after this series is finished. In Build an Excel Add-In 4 – Create the Dialog I showed a simple dialog that asked the user to identify the data to plot and select a few simple options.

In this installment of the Build an Excel Add-In series, I will show a VBA procedure that ties together all of the pieces that we’ve built so far. This master procedure is really a short list of the other procedures that do the individual tasks.

Here is the master procedure in its entirety. Below this, the individual pieces will be explained.

Sub PT_Plotter_Dialog()

  ' GET USER SPECIFICATIONS
  ' ' Using custom dialog
  ' ' Get data range and orientation
  ' ' Get header rows and columns
  ' ' Get chart type
  ' ' Get optional chart position and size

  Dim frmPlotSetup As FPlotSetup
  Dim rChartData As Range
  Dim Orientation As XlRowCol
  Dim iHeaderRows As Long
  Dim iHeaderCols As Long
  Dim ChtType As XlChartType
  Dim rPosition As Range
  Dim bCancel As Boolean
  Dim PT_Plot As Chart

  Set frmPlotSetup = New FPlotSetup
  With frmPlotSetup
    .Show

    bCancel = .Cancel
    If Not bCancel Then
      Set rChartData = .ChartDataRange
      Orientation = .DataOrientation
      iHeaderRows = .HeaderRows
      iHeaderCols = .HeaderColumns
      ChtType = .ChartType
      Set rPosition = .ChartPosition
    End If
  End With
  Set frmPlotSetup = Nothing

  If Not bCancel Then
    Application.ScreenUpdating = False

    ' make the chart
    Set PT_Plot = PT_Plotter_Chart(rChartData, Orientation, _
        iHeaderRows, iHeaderCols, ChtType, rPosition)

    ' clean up the chart
    Set PT_Plot = CleanUpChart(PT_Plot)

    Application.ScreenUpdating = True
  End If

ExitSub:

End Sub

After the declarations, the program calls the dialog to get the user’s settings.

  ' declare a new instance of the dialog class
  ' this may be overkill, but it's done this way in PED
  Set frmPlotSetup = New FPlotSetup
  With frmPlotSetup
    ' display the dialog
    .Show

    ' execution returns here when user dismisses dialog
    ' first see whether user canceled
    bCancel = .Cancel
    If Not bCancel Then
      ' if user did not cancel, we need the information
      Set rChartData = .ChartDataRange
      Orientation = .DataOrientation
      iHeaderRows = .HeaderRows
      iHeaderCols = .HeaderColumns
      ChtType = .ChartType
      Set rPosition = .ChartPosition
    End If
  End With
  ' we're done with the form, so destroy it
  Set frmPlotSetup = Nothing

Now that we’ve retrieved the user settings, it’s time to make the chart, then clean it up.

  ' skip the rest if the user has canceled
  If Not bCancel Then
    ' prevent seizures and speed execution
    Application.ScreenUpdating = False

    ' call the PT_Plotter_Chart function, pass in user info,
    ' function returns the chart
    Set PT_Plot = PT_Plotter_Chart(rChartData, Orientation, _
        iHeaderRows, iHeaderCols, ChtType, rPosition)

    ' call the CleanUpChart function to, uh, clean up the chart
    ' pass in the chart, and it is returned in cleaned state
    Set PT_Plot = CleanUpChart(PT_Plot)

    ' turn display updating back on
    Application.ScreenUpdating = True
  End If

In the next two articles, we’ll create the user interface for Classic Excel (menu and command bar elements in Excel 2000 through 2003) and for New Excel (ribbon elements in Excel 2007 and 2010). There is some clean up to do, including modifications to make the add-in work for both Classic and new Excel, and a trick to improve the chances of compatibility with as many users as possible. Finally I will cover deployment using Inno Setup, an awesome piece of customizable freeware for packaging up your add-in.

Contents: How to Build an Excel Add-In

Related Posts:

Bookmark and share this entry:
  • Twitter
  • Digg
  • Facebook
  • LinkedIn
  • del.icio.us
  • Technorati
  • StumbleUpon
  • Google Bookmarks
  • Reddit
  • MySpace
  • Slashdot
  • Yahoo! Buzz
  • Tumblr

Learn how to create Excel dashboards.

Comments


Comment from Peter H Williams
Time: Sunday, May 16, 2010, 5:21 am

Have used VBA for many , many things but never used graphs etc and found your examples very good – thankyou.

Almost immediately got something useful to work!

Small problem

I thought I’d see what happened when I loaded your full example (part 5)

I got this error on this code – what should I be doing? ( I use windows 7 VBA 6.5)

Dim frmPlotSetup As FPlotSetup

User-defined Type Not defined


Comment from Jon Peltier
Time: Sunday, May 16, 2010, 10:33 am

Peter -

In your example, FPlotSetup is not defined. In my example, FPlotSetup is the name of the UserForm that serves as the dialog for the add-in. If you’re using a dialog, its name should match the type in the Dim statement. If you’re not using a dialog, the declaration (Dim statement) is not needed.


Comment from Kurt
Time: Saturday, June 19, 2010, 5:40 am

Jon,

please continue to publish this interesting series

thanks

Kurt


Comment from Jon Peltier
Time: Saturday, June 19, 2010, 8:56 am

Kurt – Thanks for the encouragement. I plan to get back to my blog this week.

Write a comment

I welcome comments from my readers. If you have an opinion on this post, if you have a question or if there is anything to add, I want to hear from you. Whether you agree or disagree, please join the discussion.

If you want to include an image in your comment, post it on your own site or on one of the many free image sharing sites, and include a link in your comment. I'll download your image and insert the necessary html to display the image inline.

Read the PTS Blog Comment Policy.





Subscribe without commenting

Peltier Tech Waterfall Chart Utility Peltier Tech Cluster-Stack Chart Utility Peltier Tech Box and Whisker Chart Utility Peltier Tech Marimekko Chart Utility Peltier Tech Dot Plot Utility Peltier Tech Cascade Chart Utility

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