Contributed by:
Microsoft Excel VBA users should follow the basic principles or do's and don'ts.
1.
Coding Ethics in
Excel VBA Macros
Best practices
which every user should
follow
2.
1.Use Option Explicit
2. Declare variables @ start of the code
3. Declare same type of variable in one line
4. Use a data type indicator in variables
5. Don’t usesystem names as variable
6.Declare variables with interpretable
names
7. Use the line continuation character “_”
8. Use proper indentation for your code
9. Comment your code
3.
Continue to
read more to
know what they
mean ⇾
4.
01 Use Option Explicit
5.
EthicU:se Option Explicit
Why? : It forces you to declare all the
variables so that your code will
run error-free.
6.
02 Declare variables @
start of the code
7.
EthicD:eclare variables at the start
Why? : Decides the scope of the
variables upfront.
8.
03 Declare same type
of variable in one
line
9.
EthicD:eclare same type of variable in
one line.
Why? : Easy to read variables with similar
input data types.
10.
04 Use a data type
indicator in variables
11.
EthicU:se a data type indicator in
variables.
Why? : Easy to understand later which
variable holds what data type
12.
05 Don’t use system
names as variable
13.
EthicD:on’t use system/properties
names as variable names.
Why? : Using a ‘reserved’ word as a
variable can give an error.
14.
06 Declare variables
with interpretable
names
15.
EthicD:eclare variables with
interpretable names.
Why? : Easy to understand later why a
variable has been created
(purpose)
16.
07 Use underscrore
character “_” to
split longer line of
the code
17.
EthicU:se the line continuation
character – ‘Underscore’ ( _ )
Why? : ‘Line continuation’ character
makes the code line shorter, which
makes it easy to read the code.
18.
08 Use proper
indentation in
your code
19.
EthicU:se proper indentation in your
code.
Why? : Code arranged in stepped
manner (indent) is more readable
than the code written without
indentation.
21.
EthicC:omment your code for better
readability.
Why? : Helps remind you and your team
the logic and flow of the code at
the time of updating of the code.
22.
Liked our crisp post?
Follow the page
Like or Comment
if you wish to
learn more