Variables (Data Types ) in VBA ?
In this blog we will learn about Variables so called Data Types. Generally we know that every language have data types for develop codes. MS Excel have 3 Data input types (Number, Text, References) However VBA have 2 Data input types (Text and Numbers). Rather than a number everything is Text in VBA.
1. Data Types
A. Integer
D. LongLong
E. Object
F. String
G. Boolean
A. Integer
Integer is a Data Type which we assign to the integer variables. Its a Data type which can hold whole numbers without Decimals in between of Range
(-32767 To 32767). Integer Data types hold 2 bytes of memory in hard disk which is half of the Long Data type. Default value of a integer variable is 0 look at below image.
Code :
Sub IntegerDataTypeDeclaration()
Dim i As Integer
End Sub
B. Single
Single is a Data Type which we assign to the decimal variables. Its a Data type which can hold Decimal numbers in between of Range -3.4028235E+38 To -1.401298E-45 for negative values and 1.401298E-45 To 3.4028235E+38 for positive values. Single Data types hold 4 bytes of memory in hard disk which is 2X of the integer Data type. Default value of a Single variable is 0 look at below image.
Code :
Sub SingleDataTypeDeclaration()
Dim i As Single
End Sub
C. Long
Long is a Data Type which is capable to store very long data values (-2,147,483,648 to 2,147,483,648). Long only can store whole numbers (without Decimal numbers). Long variable hold 4 bytes storage of hard disk which is 2X of the integer variable. Default value of a Long variable is 0 look at below image.
Code :
Sub LongDataTypeDeclaration()
Dim i As Long
End Sub
D. LongLong
LongLong is a Data Type which is capable to store very very long data values (
-9,223,372,036,854,775,808 To 9,223,372,036,854,775,807.). LongLong only can store whole numbers (without Decimal numbers) and its available only in 64 bit version of Microsoft Office. Long variable hold 8 bytes storage of hard disk which is 2X of the Long variable. Default value of a LongLong variable is 0^ look at below image.
Code :
Sub LongLongDataTypeDeclaration()
Dim i As LongLong
End Sub
E. Object
Object is a Data Type which is capable to store any Object or its methods. It hold address that refers to Object. We can assign any reference type (String, Array , Class or interface) to an Object variable . An Object variable also can hold data of any data type (Boolean, Integer, String, Date or structure). The default value of an Object is nothing ( A Null Reference). An Object data type is a reference type however in VBA and an Object treat a value type when it refers to data of value type. Default value of a Object variable is Nothing look at below image.
Code :
Sub ObjectDataTypeDeclaration()
Dim obj As Object
End Sub
F. String
There are two types of string variables in VBA, Fixed Length and Variable length. The Fixed length variables can store string of 1 To 65400 length. The Maximum length is 65535. However Variable length string data type can store string of 0 To 2 billions length.
Fixed Length String
In the fixed length string variable declaration we can fix length of string. refer below example code to understand the same. we are taking 30 length of string on declaration time. So This string variable will be able to hold on 30 length string. Default value of a Fixed Length String variable is "0000..." look at below image.
Code :
Sub FixedLengthStringDataTypeDeclaration()
Dim FixedLength As String * 30
End Sub
Variable Length String
Variable length string can hold 0 To 2 Billions characters string. Default value of variable string is empty (""). So String variable do automatically increase or decrease variable size as per inputs.
A variable length string can hold string of any length. Code for declare string variable is as mentioned below. Default value of a Variable Length String variable is "" look at below image.
Code :
Sub VariableLengthStringDataTypeDeclaration()
Dim varstr As String
End Sub
G. Boolean
Boolean is variable in VBA which can only hold Boolean values (TRUE or FALSE). Its also a inbuild data type in VBA. This use for hold only logical references or logical variables which is use for logical comparison. Default value of a Boolean variable is FALSE, The declaration of this data type is as mentioned below.
Code :
Sub BooleanDataTypeDeclaration()
Dim bln As Boolean
End Sub
No comments:
Post a Comment
Feel free to ask your questions regarding Ms Excel & VBA macros.