
August 29th, 2007
|
 |
Administrator
|
|
I'm a Spammer: NO
MS Office Version: 2007 English
Op System: Windows Vista
Assumed Experience: Above Average
Join Date: 24th January 2003
English is 1st Language: Yes
Location: Australia
|
|
|
Excel/VBA Golden Rules. These Should NOT Be Optional
GOLDEN RULES
Also See Excel Best Practices- Never Use Manual Calculation Mode, It's a False Reading Waiting to Happen! If You are Forced to use Manual Calculation, you HAVE a Bad Spreadsheet Design Which Should be Fixed, not Catered to.
- 1 Worksheet For ALL Related RAW Data.
- Classic Table Format For Related Data. That is, Row 1 For Headings and Corresponding Data Underneath.
- Don't Mix Raw Data With Final Results, Reports or Data.
- No Blank Cells in a Table.
- No Merged Cells (Use Center Across Selection Instead).
- Avoid Empty Text ("") For Formula Results, Use Zero Instead. Tools>Options - View - Zero Values to Hide Globally. Or, Custom Format Like: 0;-0; To Hide Cell-By-Cell.
- Avoid Array Formulae and Multiple Criteria SUMPRODUCT. Make Use Of PivotTables and/or Database Functions.
- Don't Nest 2 Lookups To Avoid #N/A! Allow it to Occur and Reference Like: =IF(ISNA(A1),0,A1) and Hide the Lookup Column
- Avoid Deleting Rows and Make Use of Auto Filter (AutoFilter), AdvancedFilter or Sort.
- Avoid Volatile Formulae (especially for current date and/or time. NOW, TODAY etc). E.g If Current Date is Needed in Multiple Formulae, Add =TODAY() Into a Cell You Have Named Today. Then, in Your Formulae use Today in Place of TODAY().
- Avoid Formatting Cells as Text. Very Rarely is a Text Format Needed.
- Never Store Numbers as Text.
- Avoid Changing the Default Horizontal Alignment of Cells. Numbers, by Default are Right Aligned, While Text, by Default, is Left Aligned.
- Always use True Dates & Times That Excel Will Recognize as Such. Even Headings for Things Like Month & Day Names. Use True Dates and Custom Format as MMMM or DDDD.
Excel Formula Errors
VBA CODE GOLDEN RULES- Turn on Option Explicit. Tools>Options - Editor - Require variable declaration in the VBE. Then Educate Yourself on Their Proper Use and Data Types.
- Split Procedures Into Logical Seperate Procedures and use Call or Run When Needed.
- Make Good use Of Functions so They Can be Called When Needed and With Variable Elements Passed.
- Try & Keep all Related Procedures in the Same Module. Excel Compiles Each Module as a Procedure in the Module in Run.
- You Rarely Need to Select or Activate any Objects to Change Their Properties or Access Their Methods. If you MUST Select a Range Object, Use GoTo.
- Use a Sheet CodeName Over The Tab Name or Index Number.
- Avoid Loops. Make Good use of Much Faster Alternatives Like Find (Find Method), AutoFilter, AdvancedFilter, SpecialCells etc.
- Loops Through Object Collections are the Fastest, Compared to Other Loop Types.
- Don't Assume Code is Needed. Often a Worksheet Function is FAR better and More Efficient.
- Avoid Using Custom Functions When a Built-in Worksheet Function can be Used. Even Deeply Nested Worksheet Function are Often a Lot More Effiecient Than Custom Function Written in VBA.
- Avoid Macros That Delete Rows/Column/Cells. Make Use of AutoFilter, AdvancedFilter or Sort.
- Turn Off Calculations via Code for Slow Macros. See Macro Code via Manual Calculation. Assume Your Macro Will Fail, so Turn Back on In any Error Trapping.
- Use VbNullString Over ""
- Turn off Sheet/Workbook Events if They are Not Needed While Macro is Running. That is, Application.EnableEvents = False and Don't Forget to Turn Back on and Assume Your Macro Will Fail, so Turn Back on In any Error Trapping.
- Make Good use of With Statements When Working With Objects.
- Select Case is Often Better Than Mutiple If Else Statements.
- IIf is Slower Than If Else.
- Use Boolean Logic Over If Statements. E.g bYesNo = Range("MyValue") = 5.
- Use Named Ranges Over Cell Addresses.
- Use Meaningful Variable Names and Precede Them With Their Data Type. E.g lngRowCount NOT Meaningless Names Like x, y, z etc.
- Capitalize at Least 1 Character in Variable Names and When Using in a Procedure use ALL Lower Case. Excel Will Convert as To the Case Used When Dimensioned.
An Explanation of Trappable Errors in Visual Basic for Apps
Last edited by Dave Hawley : January 6th, 2009 at 15:45.
|