Visual Basic/Data Types

Data types in Visual Basic can be divided into three groups:


 * Native: Types that are understood directly by the Visual Basic compiler without assistance from the programmer
 * User-defined: commonly referred to by the initials UDT, meaning User defined Type, these correspond to Pascal records or C structs
 * Classes: the basis for object oriented programming in Visual Basic. Classes include forms, add-ins, and database designers.

If you are using Excel VBA, remember that VBA is essentially VB6, and hence those rules apply.

Built in Types
The built in types are:



Byte, Integer & Long
Example:

Now those variables will only be capable of storing integer values (without decimal). Long integers can store a number with a bigger range of value than integers but they occupy a bigger space of RAM.

Some functions you need to know: Int

Int converts a decimal value into an integer value:

Single & Double
These data types can store decimal values. "Double" compared to "Single" is similar to the "Long" compared to "Integer":

Some useful functions: Round

Round rounds off a decimal to a certain number of decimal digits that the programmer wants. The first argument should be a decimal value which you want to round off. The second argument specifies the number of decimal digits you want, for example:

String
A string is an array of characters. As an example:

Strings can be concatenated (connected together to form a new string) using the "&" operator. For example,

A normal string variable occupies 10 bytes of RAM, plus the string's size, and can hold up to 2 billion characters!

Some frequently used built-in string constants: vbTab, vbCrLf

vbTab contains a string that does the same thing as the Tab key on your keyboard, while vbCrLf creates a character return and a line feed(similar to the Enter key):

Will print:

Jack:   1 pie me:     10 pies

To include special characters and quotation marks in the strings, the Chr function may be used:

Some string functions: Str,Val,inStr,Mid,Replace,Trim

In fact there are tons of built-in string manipulation functions available. But right now, I'll just introduce two: Str and Val.

Str converts any numerical value into a string value while Val converts a string value into a numerical value(only when it's convertible).

Even if you don't do the conversion, you will not end up getting Type Mismatch Errors. However, it is considered better to use explicit type conversions, because it is easier to debug.

Even if you do be prepared for next to impossible to debug problems caused by VB refusing to convert something and refusing to tell you what the heck it is. VB is extremely touchy and raises an exception at the least expected times.

Structure
An example definition of a structured type: The type has to be defined outside of a procedure.

A variable of a structure type is not an object.

Links:
 * Type Statement, Office 2013, msdn.microsoft.com
 * Type Statement at Visual Basic for Applications Reference, msdn.microsoft.com

Enumeration
An example definition of an enumerated type:

Links:
 * Enum Statement, Office 2013, msdn.microsoft.com
 * Enum Statement at Visual Basic for Applications Reference, msdn.microsoft.com

Type Test
To find out about the type of a variable, you can use "TypeOf ... Is ..." test. The tested types can only be object types and structure types, but the test can be applied to any variable, whether typed as an integer, a string or an object.

An example of TypeOf in Excel:

An example test with a "Select Case True":

You can further find out about a type using IsObject and TypeName functions:

Links:
 * If...Then...Else Statement at Visual Basic for Applications Reference, msdn.microsoft.com
 * TypeName Function at Visual Basic for Applications Reference, msdn.microsoft.com
 * IsObject Function at Visual Basic for Applications Reference, msdn.microsoft.com
 * IsArray Function at Visual Basic for Applications Reference, msdn.microsoft.com
 * IsDate Function at Visual Basic for Applications Reference, msdn.microsoft.com
 * IsNumeric Function at Visual Basic for Applications Reference, msdn.microsoft.com
 * IsNull Function at Visual Basic for Applications Reference, msdn.microsoft.com