In most programming languages, data types are specified for each variable, such as String, Integer, Date, Boolean, etc. In VBA, you specify the data type when you declare a variable with the As keyword:
Dim BabyFirstWord As String
BabyFirstWord = “Googoo”
To create concise and fast code, it is recommended to explicitly specify a data type. If you use Option Explicit it will force all of your variables to have explicit data types. You can of course declare a variable without specifying a data type. In this case, VB assigns the variable to a special data type called Variant.
Variants
When you do not specify a data type for a variable, the variable is assigned to the data type called Variant. The Variant is a special type of variable that can store a value of any type. Depending on the value and the context VBA will automatically assign the Variant one of the following sub-types: Null, Numeric, String, Date/Time, Boolean, or Object.
A NULL subtype is an unusual subtype that is used in conjunction with databases. NULL refers to a field that contains no data. NULL does not mean zero, or even empty. Zero is a valid value, and empty is a data type that has nothing assigned to it yet. NULL means “nothing”, no data and no data type.
Numeric Data Types
Numeric Data Types include the following:
-
Integer – Whole numbers within the range of -32,768 to 32,767.
-
Byte – Integers within the range of 0-255
-
Long – Similar to Integer, but supports ranges from -2,147,483,648 to 2,147,483,647
-
Single – A single precision floating point number
-
Double – A double precision floating point number
-
Currency – Accepts numbers up to four decimal places
String Data Type
A string holds textual information or words. String values are usually put in quotes. You cannot perform mathematical functions on strings, even if numbers are present in the string:
Number1 = “12”
Number2 = “14”
Number3 = Number1 + Number2 ‘Will Produce “1214”
Date/Time Data Types
Date/Time data types store dates and times, and must be enclosed in # characters:
myBirthDate = #5/4/1965#
Boolean Data Types
Boolean data types store values as either TRUE, or FALSE.
Object Data Types
The Object data type stores a reference to an object. Objects will be discussed further a bit later in this supplement.
Naming Variables
It is important to establish a good naming convention for your variables. Your naming convention should be easily understood by another developer who might look at your code later. Some tips for naming:
NameFirst and NameLast are better than FirstName and LastName because they will appear together in a search.
Within a procedure or function you may want to prefix your variables with a p_ or f_ type code letter.
Create names with multiple words, capitalizing the first letter of each word.
The Hungarian notational convention is to use the first three letters of the variant’s name to distinguish the sub-type, as shown in the following table:
Data Type
|
Prefix
|
Example
|
Boolean
|
bln
|
blnMember
|
Byte
|
byt
|
bytByte
|
Date / Time
|
dat
|
datToday
|
Double
|
dbl
|
dblDouble
|
Integer
|
int
|
intSalary
|
Long
|
lng
|
lngLong
|
Object
|
obj
|
objConn
|
Single
|
sng
|
sngSingle
|
String
|
str
|
strTextBox
|
For your applications, you do not have to use the Hungarian notation, but something similar is recommended.
Note:
|
Naming conventions and coding style standards evolve over time. Check if your organization has a defined naming convention and coding standard to use.
|
In order for variables to be of any use, you will need to manipulate them in some way. You can manipulate variables using a number of different operators and by concatenating or converting them.
Assignment Operator
The assignment operator is the ‘equal’ (=) sign. With it, you can assign a value to variable. The variable name goes on the left, and the value to be assigned on the right side of the equals sign.
intMyFavoriteNumber = 11
The assignment operator can also be used to assign a new value to a variable, as in the following mathematically unsound formula:
intMyFavoriteNumber = intMyFavoriteNumber + 1
In the above case, it may look like we are saying 11 = 11 + 1. However, we are simply reassigning a new value to intMyFavoriteNumber.
Comparison Operators
Comparison Operators are the following:
-
Equality: =
-
Inequality: <>
-
Less than: <
-
Greater than: >
-
Less than or equal to: <=
-
Greater than or equal to: >=
Arithmetic Operators
You can use the following arithmetic operators to perform mathematical operations on your variables:
-
Addition: +
-
Subtraction: -
-
Multiplication: *
-
Division: /
-
Exponentiation: ^
-
Negation: -
-
Modulus: MOD or \
Share with your friends: |