Download presentation
Presentation is loading. Please wait.
1
Spreadsheet Models for Managers
Session 15 Spreadsheet Tools for Managers Command Macros Templates Distribution within the Enterprise Last revised: November 16, 2009
2
Review of last time Iteration Dynamic arrays
Using the Set statement for objects
3
Command macros Command macros “do” things to Excel or to workbooks
Define a name Color a cell blue Open a workbook Anything (almost anything) that you can do with keyboard or mouse Use the macro recorder to see what command macros look like Recorder is a fast way to learn how to do something Also use to “rough out” your macro
4
Example: Create a worksheet named “info”
Turn on recorder, insert a new worksheet, name it “info” Stop recorder, open Visual Basic Editor But the macro recorder isn’t a very good programmer. Here’s what a programmer would write: Sub Macro1() Sheets.Add Sheets("Sheet4").Select Sheets("Sheet4").Name = "Info" End Sub Sub Macro1() Sheets.Add.Name = "Info" End Sub Usually, when you record, you’ll have some cleanup to do
5
Many commands aren’t recordable
For an example, turn on the recorder and open a file The recorded macro opens that specific file But a common need is a command macro that enables the user to specify a file To learn about these methods and statements use these references: Excel-L discussion list Various books Sub TypicalUse() Dim theFile theFile = Application.GetOpenFilename If theFile = False Then Exit Sub <…various other steps…> End Sub
6
What command macros should you build?
Commands that are expensive If done incorrectly If you were to train (and retrain) people to do them If people have to do them by hand So build command macros for procedures that: Are complex Are used reasonably often Must be done right One other category: doing things quickly High throughput can be a competitive asset It can enable new ways of doing business Usual structure: automatic polling of workbooks across the intranet
7
Presentation vs. maintenance
Often we must present our data or models The needs of presentation sometimes conflict with ease of maintenance Examples: M1 M2 M3 Q1 M4 M5 M6 Q2 M7 M8 M9 Q3 M10 M11 M12 Q4 Y Q1 Q2 Q3 Q4 Y1 Q1 Q2 Q3 Q4 Y2 Q1 Q2 Q3 Q4 Y3 The above display is popular, especially with Finance It also makes maintenance difficult: The formulas for year data are often different from quarter data Edit>Fill Right is impossible Audit tools are thrown off Alternate approach: split presentation from maintenance Create the data on one sheet Display it on another Use a macro to compute display on a separate sheet Either a command macro or a function macro can work
8
Templates A workbook template contains infrastructure for a specific task You could have made a template for your homeworks Main application: data collection across an organization or across time Advantages Reduce setup costs Regularize data to support command macros and automation Mechanize “roll-ups” How to make a template Set up the workbook the way you want it Save as template If you save to the templates directory, it’s available in the Templates list
9
Enterprise templates When you distribute a template throughout the organization You can’t anticipate everything You’ll probably have to issue updates People will someday have to transfer data from the old template to the new To support updates Version stamp the template Provide macros to bring data forward to new versions Avoid having submitters enter formulas Avoid including macro tools in templates Distribute macros as add-ins instead Enables updating macros without affecting data Knowing that the data never contains macros makes virus detection easier
10
Macros for updating templates
Typical reasons for updating templates Need a new data range Incorrect formula Need new name New value for a parameter New parameter These situations don’t affect existing data When existing data isn’t compromised, you can easily write an update macro Pull data from a workbook made from old template Insert that data into a workbook made from new template
11
An update macro Sub CopyData(oldWorkbook As Workbook, newWorkbook As Workbook) Dim namedRegions, i As Integer Dim rangeOld As Range, rangeNew As Range namedRegions = Array("InitialHeadcount", "Hiring", "Turnover") For i = 1 To Ubound(namedRegions) Set rangeOld = oldWorkbook.Worksheets("Sheet1") _ .Range(namedRegions(i)) Set rangeNew = newWorkbook.Worksheets("Sheet1") _ rangeNew.Value = rangeOld.Value Next i End Sub
12
Example: Updating a template
Create a tool that asks the user to specify a source file and a destination file, and copies data from the source to the destination. The data is held in the three ranges called “InitialHeadcount”, “Hiring”, and “Turnover” Make your macro accessible by inserting a button onto the worksheet, and attaching the macro to it.
13
Add-Ins An Add-in is a workbook in a special format
Enables you to “extend” Excel Use add-ins to distribute macros, styles, chart formats, toolbars, etc. Main advantage: you can update features without affecting user data Always version-stamp add-ins
14
Key bindings for macros
To attach a macro to a keyboard shortcut, add this to the ThisWorkbook module: Read about the key specifiers in on-line help for VBA The macro specifier is just the name of the macro as a string Sub Workbook_Open() Application.OnKey <key-specifier>, <macro-name> End Sub Sub Workbook_BeforeClose(ignore As Boolean) Application.OnKey <key-specifier>
15
Tips for advanced users
The defaults of some Excel options are helpful for novices But more advanced users should use different values Edit directly in cell: turn off Show Visual Basic toolbar (Excel 2003 and 2004) Add Developer>Visual Basic command to Quick Access Toolbar (Excel 2007) Manual recalculation Use templates Use chart formats
16
The main points Rough out your command macros using the macro recorder
Many commands can’t be recorded Separate the presentation function from the maintenance function Using the Set statement for objects Use templates to collect data from the organization Avoiding putting macros in templates Distribute macro collections as add-ins
17
Reference readings Rob Bovey, Stephen Bullen, John Green, Robert Rosenberg, Excel 2002 VBA Programmers Reference. Birmingham, UK: Wrox Limited. On line help for VB takes some getting used to, but it is serviceable. Readings: Excel Macros in Visual Basic for Applications
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.