Excel VBA Best Practice: Simon Murphy Developer - Codematic LTD
Excel VBA Best Practice: Simon Murphy Developer - Codematic LTD
Simon Murphy
simon.murphy@codematic.net
Developer Codematic Ltd
UK XL User
Conference 2006
Spreadsheet background
Up to 200 Mb size
Up to 1 Million formulas
1-10,000 unique formulas
5-10,000 lines of VBA
Billions in values
Often linked to other technologies such as OLAP, ADO,
COM or .net etc.
Finance, Banking and Sales and Marketing areas
Development cost up to $1M
Active member of Eusprig European Spreadsheet Risk
group dedicated to raising awareness of dangers and
error rates in commercial spreadsheets www.eusprig.org
UK XL User
Conference 2006
Security
Fundamental Imperative
Development process
Development environment
Design considerations
High quality code
General
Modules
Procedures
Blocks
Variables
Excel Specific advice
Classes
UK XL User
Conference 2006
Agenda
Security
Is everybodys concern
Spreadsheets can be used as a staging board for privilege escalation (with
your login details!)
Consider SD3 +C
Secure by
Design
Default
Deployment
Communication
UK XL User
Conference 2006
Fundamental Imperative
Manage complexity (McConnell)
Solution complexity grows at 4 x the rate of
problem complexity. (Glass)
[Things] should be as simple as they can be, but
no simpler (Einstein)
K.I.S.S.
This principle should drive all other work.
Easier to build, easier to test, easier to
document, easier to use, etc [No conflict]
UK XL User
Conference 2006
Development Process
Requirements,
Analysis,
Logical Design,
<Technology Choice>,
Physical Design,
Construction,
Test,
Release,
Maintain.
In some shape or form.
2 Approaches:
What will the system do?
What real world objects am I modelling?
UK XL User
Conference 2006
Coupling
How strongly 2 separate elements depend on
each other
Low coupling is better, especially through clearly
defined interfaces
High coupling often means hidden dependencies
which generally leads to incorrect modifications
(side effects)
Example: hard coded cell addresses in VBA code
Example: Gobal/Public variables
UK XL User
Conference 2006
Development Environment
Option explicit
Dont warn about syntax errors
Actual set up - which windows are visible etc
Use a code library
Use MZ tools
Other tools call tree, indenter, reference
checker, BYG
Source control, even just exporting as text
UK XL User
Conference 2006
Design Considerations
Simplicity
Testability
Cohesion and coupling
Fan in fan out
Encapsulation and separation
Reusability
What does it do, or what are you modelling?
N tier especially msgbox and error handling
Consider future changes and/or enhancements
Design patterns eg: singleton, factory, facade
Design normal case and error case
UK XL User
Conference 2006
Naming convention:
Option 1 fit in with current standards (eg Reddick 3 letter)
Option 2 give everything a simple, distinctive, non abbreviated descriptive
name
Procedures verb noun combination
Variables add scope prefix, data type maybe, usage more useful eg
m_rwCustomer for row in customer table or colMonth for month
column.
Only use a type prefix if you know what it is doing for you and you value that.
(eg probably dont need obj)
Avoid most comments make meaning crystal clear in the code.
Move non standard code and workarounds to named routines, and comment if
necessary
refactor early and often
build test routines as you go, and at least step through in the debugger.
UK XL User
Conference 2006
UK XL User
Conference 2006
UK XL User
Conference 2006
UK XL User
Conference 2006
UK XL User
Conference 2006
UK XL User
Conference 2006
UK XL User
Conference 2006
UK XL User
Conference 2006
UK XL User
Conference 2006
UK XL User
Conference 2006
Immediate window
Locals
Debug.print debug.assert
Testing
UK XL User
Conference 2006
Documentation
If you design for simplicity only very minor additional
documentation is needed
Design and build the user Interface with the Users needs
primary, the documentation will automatically be there
Design and build the business logic parts with the
maintainers needs primary, the main documentation will
automatically be there, expand as required.
Integral tests should explicitly clarify intent.
Reports should contain enough description to be
meaningful.
External documentation is almost always so out of date its
worse than useless.
Excessive documentation is too hard to plod through
Poorly targeted documentation is pointless
Working software is more useful than documentation
Sometimes documentation is important
UK XL User
Conference 2006
Documentation
Call tree print out should be enough for most uses
Be clear who the target audience is
Should be auto generated from source code if it is to be up
to date.
Stepping through code is often the easiest way to
understand it.
Download a VBA to HTML addin to pretty up code for
printing.
PUP provides a nice summary, MZ Tools creates a weighty
document.
UK XL User
Conference 2006
Extending Excel
Excel is powerful not perfect
Leverage benefits whilst managing weaknesses
by using complimentary technologies
Data: VBA, ADO, ODBC, OLAP, .net, COM, Info
Bridge, XML, DDE, Web Queries, SOX Solution
Accelerator
Logic: VBA, COM, .net, xll, Pivots, Filters
Search the web for vast array of free or cheap
tips and tools.
UK XL User
Conference 2006
Summary
Manage complexity
Consider security carefully
Excel/VBA not the best tool for everything
Be defensive, especially with inputs
Understand your data
Aim for cohesive models with low coupling
Use names to connect VBA to worksheets
Use complimentary technologies where
appropriate
Manage documentation
UK XL User
Conference 2006
Resources
Books
Tools
UK XL User
Conference 2006
Questions?
simon.murphy@codematic.net
Spreadsheet consulting, reviewing,
maintaining, rescuing, migrating, add-in
development etc.
Staff coaching, mentoring and training
Websites
www.codematic.net
www.xlanalyst.co.uk
UK XL User
Conference 2006