Spreadsheet Design Concepts Series
- VBA UDFs should get all range info via parameters
- Where (non UDF) VBA uses worksheet ranges, these should almost certainly be named ranges.
- Sheet1.Range("Inflation").value = 0.02, or Sheet1.[Inflation].value = 0.02, rather than
- Sheet1.[C5].value = 0.02, which may become invalid if rows or columns are inserted or deleted
- strInflationRange = "C5" is just as bad - it creates a hidden dependency that must be manually updated when worksheet changes (poor coupling)
- Variable and routine names:
- Use very meaningful names (8-30 chars length)
- Use a simple naming convention (matched pairs)
- Use scope prefixes (g, m), and minimise it
- Don't have to use data type variable prefixes (s, str, l, lng etc) (see .net advice)
- avoid abbreviations
- Don't use code comments demo make code ultra clear instead
- Option Explicit on
- Avoid Application.Run (non VBA - breaks error management)
- routines 1 screen long max
- 3-4 levels of nesting max
- Use source control
Fuller information about VBA Best Practice (according to Codematic, of course - everyone has their own view) is here.
Of course all this is great in theory, but one of the key elements of professionalism is knowing when to go against so called best practice.
25 January 2012 - UK Excel Developer Conference - London
Products for sale:
New information about the missing FileSearch feature in Office 2007 and details of our pragmatic solution (Current price GBP 30.00)
Instant Excel worksheet protection remover and password recovery (Current price GBP 15.00)
Classic Ribbon Tab
Add Excel 97/2000/2002/2003 compatible menu structure to Excel 2007
(Current Price GBP 10.00)
Products coming soon:
(Find and control external links in Excel Workbooks)
Due by Q1 2111.
(Excel VBA based spreadsheet auditing tool)
Due before the end of 2111.