Excel VBA Programming Golden Rules
Excel VBA Programming Golden Rules
Excel VBA Programming Golden Rules
www.masterofmacros.com/login
Contents
1 Introduction ............................................................................ 5
2 How to make your macros easier to write, read, remember AND
Maintain .................................................................................... 6
2.1 Design the algorithm first .................................................... 6
2.2 Make your Excel VBA Macro project modular........................ 10
2.3 Name all your VBA elements purposefully (naming convention)
........................................................................................... 13
2.3.1 General guidelines for naming Subs, Functions, Modules . 13
2.3.2 General guidelines for naming Variables ........................ 14
2.3.3 General guidelines for naming Objects and form controls . 15
2.4 Document the macro ........................................................ 17
2.4.1 Add the main Macro information ................................... 18
2.4.2 Specify the purpose of the macro ................................. 19
2.4.3 Specify the purpose of any significant line or block of code
........................................................................................ 20
2.5 Make the code flow........................................................... 20
2.5.1 Indenting................................................................... 20
2.5.2 Line breaks ................................................................ 22
3 How to make macros/Functions/statements dynamic ................. 23
3.1 Put changing-information outside ....................................... 24
3.1.1 Placeholders in Subs & Functions .................................. 24
3.1.2 Placeholders in VBA statements and expressions ............ 26
4 How to write macros optimized for speed and memory ............... 27
4.1 Screen update ................................................................. 27
www.masterofmacros.com/login
www.masterofmacros.com/login
1 INTRODUCTION
www.masterofmacros.com/login
www.masterofmacros.com/login
The point is
You cannot improve a macro by just tweaking a line of code here and
there; see the ATM example below
For these variables
CustomerAmount = $100
CustomerFunds = $500
www.masterofmacros.com/login
But the macro will remain poor; it will perform two checks instead of
only one.
So, to make a macro flow so requires less code and is easily
understood, you need to make disruptive changes (not incremental
ones), and this is done at the logic level.
www.masterofmacros.com/login
www.masterofmacros.com/login
10
www.masterofmacros.com/login
11
www.masterofmacros.com/login
12
OpenWorkbook
CloseExcel
DeleteFormat
ChangeColorCell
ChangeColorFont
13
Prefix Example
Module-level
mstrCompName
intNum
Prefix Example
Boolean
bln
blnFound
Currency
cur
curRevenue
dtmStart
Double
dbl
dblTolerance
Integer
int
intQuantity
www.masterofmacros.com/login
14
Long
lng
lngDistance
Single
sng
sngAverage
String
str
strFName
Variant
vnt
vntCheckSum
prefix Example
Check box
chk
chkReadOnly
cboEnglish
Command button
cmd
cmdExit
Form
frm
frmEntry
Frame
fra
fraLanguage
Label
lbl
lblHelpMessage
Line
lin
linVertical
List box
lst
lstPolicyCodes
Option button
opt
optGender
Picture box
pic
picVGA
www.masterofmacros.com/login
15
Shape
shp
shpOctagon1
Text box
txt
txtLastName
www.masterofmacros.com/login
16
www.masterofmacros.com/login
17
www.masterofmacros.com/login
18
19
20
www.masterofmacros.com/login
21
www.masterofmacros.com/login
22
Now, the price is not on the bottle but on some central database. You
add barcode labels on bottles 1000 times; the same number of times
as the price tags, but heres the great news
You can update the price of 1000 bottles by making just one change
at the central database.
Now you have fun!
So when your boss asks you to change the product price, you just
change the respective database entry from $1.45 to $1.46 and all
www.masterofmacros.com/login
23
24
On the other hand, this macro prints any range of pages and any
amount of copies each time
www.masterofmacros.com/login
25
www.masterofmacros.com/login
26
27
28
29
Storage size
1 byte
2 bytes
2 bytes
4 bytes
8 bytes
4 bytes on 32-bit
systems, 8 bytes
on 64-bit systems
Single (single-precision
floating-point)
4 bytes
Double (double-precision
floating-point)
8 bytes
Decimal
14 bytes
Date
Object
String (variable-length)
String (fixed-length)
8 bytes
4 bytes
10 bytes + string
length
Length of string
16 bytes
www.masterofmacros.com/login
Range
0 to 255
True or False
-32,768 to 32,767
-2,147,483,648 to 2,147,483,647
-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807 (Valid on 64-bit
platforms only.)
-2,147,483,648 to 2,147,483,647 on 32-bit
systems, -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807 on 64-bit
systems
-3.402823E38 to -1.401298E-45 for negative
values; 1.401298E-45 to 3.402823E38 for
positive values
-1.79769313486231E308 to 4.94065645841247E-324 for negative
values; 4.94065645841247E-324 to
1.79769313486232E308 for positive values
-922,337,203,685,477.5808 to
922,337,203,685,477.5807
+/-79,228,162,514,264,337,593,543,950,335
with no decimal point; +/7.9228162514264337593543950335 with 28
places to the right of the decimal; smallest
non-zero number is +/0.0000000000000000000000000001
January 1, 100 to December 31, 9999
Any Object reference
0 to approximately 2 billion
1 to approximately 65,400
Any numeric value up to the range of a
Double
30
22 bytes + string
length (24 bytes on
64-bit systems)
Number required
by elements
www.masterofmacros.com/login
31