Declaring

The first character must always be alphabetic.
You cannot use spaces or periods.
Variable names can use alphabetical characters, number and some punctuation characters but not ( * . , # $ % & !).
The use of the underscore character is also encouraged.
The name must start with a letter and can have a maximum of 254 characters.
A variable must begin with a letter or an underscore
Variable names can be constructed from letters and numbers and the underscore character.
Variable names cannot be the same as VBA keywords.
It is always a good idea to type VBA keywords in lower case and then wait for them to automatically change.


Should they be declared at the very top or inline ??


This statement is valid although the variable sFirstName is defined as a Variant variable and not as a string variable.

Dim sFirstName, sLastName As String 

VBA does not let you declare a group of variables to all be of the same datatype.
You must specify the type of each variable explicitly.
You should always try and declare each variable on a separate line

Dim sFirstName As String 
Dim sLastName As String

You could put this on one line

Dim sFirstName As String, sLastName As String 

Initialising Variables

When a procedure or function begins execution, all of its local variables are automatically initialised, that is given an initial value.
Explicitly initialising all your local variables makes your code more readable and is a good habit to get into.
These default values can be found in the datatypes table earlier.


No Default Values

In C# there are no default values assigned to your variables.
C# does not assign any default values to local variables when they are declared.
Instead it requires you to always initialise the variables before they are used
A compilation error will occur if you try to read a value from a local variable before it has been initialised.


VB however assigns default values to all its local variables.
VB automatically initializes all variables to 0 or nothing.


When a procedure or function begins execution, all of its local variables are automatically initialised, that is given an initial value.
Explicitly initialising all your local variables makes your code more readable and is a good habit to get into.
Static variable however cannot be initialised, due to there fundamental purpose.
These default values can be found in the datatypes table


Numeric variables (integer, short, long, double, currency) are initialised to zero
Variable length strings are initialised to a zero-length (or empty) string
Fixed length strings are initialised with the character represented by the ASCII code 0, or Chr(0).
Variant variables default to Empty
Object variables default to Nothing


When a subroutine starts all the local variables are automatically initialised to their default values.


Dim sFirstName As String 
'the automatic initialisation will set its initial value to the empty string ""

It is not good programming practice to rely on these automatic initial values.
You should always try and explicitly specify the initial value.

Dim sFirstName As String 
   sFirstName = ""



© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext