Sponsored Link

There was an error in this gadget

Sponsored Link

Wednesday, December 10, 2008

VARIABLE Declaration in VBA

This will be shown only in post's page. Variable is used to store temporary information that is used for execution within the Procedure, Module or Workbook. Before we go into some detail of Variables, there are a few important rules that you must know about.

1) A Variable name must Start with a letter and not a number. Numbers can be included within the name, but not as the first character.
2) A Variable name cannot be longer than 250 characters.
3) A Variable name cannot be the same as any one of Excel's key words. By this, I mean you cannot name a Variable with such names as Sheet, Worksheet etc.
4) All Variables must consist of one continuous string of characters only. You can separate words by either capitalising the first letter of each word, or by using the underscore characters if you prefer.

You can name variables with any valid name you wish. For Example you could name a variable "Charles" and then declare it as any one of the data types shown below. However, it is good practice to formalize some sort of naming convention. This way when reading back your code you can tell at a glance what data type the variable is. An example of this could be the system I use! If you were to declare a variable as a Boolean (shown in table below) I may use: bIsOpen I might then use this Boolean variable to check if a Workbook is open or not. The "b" stands for Boolean and the "IsOpen" will remind me that I am checking if something is open.

You may see code that uses letters only as variables, this is bad programming and should be avoided. Trying to read code that has loads of single letters only can (and usually does) cause grief.

Mentioned below are the data types that can be used in VBA

Byte data type
A data type used to hold positive integer numbers ranging from 0 to 255. Byte variables are stored as single, unsigned 8-bit (1-byte) numbers. Useful when you know the maximum value that you want to store the in the variable.
Example below shows how to declare byte variable.

Dim btValue As Byte

Boolean data type
A data type with only two possible values, True (-1) or False (0). Boolean variables are stored as 16-bit (2-byte) numbers. Useful when you want to check certain value return after testing. Also, useful in controlling the program flow and you declare boolean its value is false by default

Dim blTest as boolean

Integer data type
Integer is most commonly used variable. A data type that holds integer variables stored as 2-byte whole numbers in the range -32,768 to 32,767. The Integer data type is also used to represent enumerated values. The percent sign (%) type-declaration character represents an Integer in Visual Basic.

Dim intCount As Integer

Long data type
A 4-byte integer ranging in value from -2,147,483,648 to 2,147,483,647. The ampersand (&) type-declaration character represents a Long in Visual Basic.

Dim lngCount As Long

Currency data type
A data type with a range of -922,337,203,685,477.5808 to 922,337,203,685,477.5807. Use this data type for calculations involving money and for fixed-point calculations where accuracy is particularly important. The at sign (@) type-declaration character represents Currency in Visual Basic.

Dim cur_profit As Currency

Single data type
A data type that stores single-precision floating-point variables as 32-bit (2-byte) floating-point numbers, ranging in value from -3.402823E38 to -1.401298E-45 for negative values, and 1.401298E-45 to 3.402823E38 for positive values. The exclamation point (!) type-declaration character represents a Single in Visual Basic.

Dim sngValue As Single

Double data type
A data type that holds double-precision floating-point numbers as 64-bit numbers in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values. The number sign (#) type-declaration character represents the Double in Visual Basic.

Dim dbValue as Double

Date data type
A data type used to store dates and times as a real number. Date variables are stored as 64-bit (8-byte) numbers. The value to the left of the decimal represents a date, and the value to the right of the decimal represents a time.

Dim dtStart as date

String data type
A data type consisting of a sequence of contiguous characters that represent the characters themselves rather than their numeric values. A String can include letters, numbers, spaces, and punctuation. The String data type can store fixed-length strings ranging in length from 0 to approximately 63K characters and dynamic strings ranging in length from 0 to approximately 2 billion characters. The dollar sign ($) type-declaration character represents a String in Visual Basic.

Dim strName as String

Object data type
A data type that represents any Object reference. Object variables are stored as 32-bit (4-byte) addresses that refer to objects. Variant data type A special data type that can contain numeric, string, or date data as well as the special values Empty and Null. The Variant data type has a numeric storage size of 16 bytes and can contain data up to the range of a Decimal, or a character storage size of 22 bytes (plus string length), and can store any character text. The VarType function defines how the data in a Variant is treated. All variables become Variant data types if not explicitly declared as some other data type.

One small example of variable in macro is written below.

Sub VariableMacrotest()
Dim intEntervalue As Long
'Sheet1.Cells(1, 1) refers to A1 on sheet1
intEntervalue = Sheet1.Cells(1, 1)
MsgBox intEntervalue * intEntervalue
End Sub

To use the above code, press Alt + F11 to open the Visual Basic editor in MS Excel. If you don’t see the project explorer which by default on right hand side. Press Ctrl + R key. You will see the sheet MS Excel Worksheets. Right Click on the VBAProject(Workbookname) and go to insert and click on the module.
Copy past the code in the module and press F5 to run the macro/vba code.

Here I have attempted to put light on variable type available in MS Excel. I am sure this post was helpful. There are few more example I will post on variable in coming days.
 
Kindly subscribe to our blog via email to receive latest updates, its free.
 
We assure you knowledge, not SPAM!

1 comments:

Life said...

How to lock a single row or column.

How to restrict the other user to edit a single row or a cloumn in MS Excel-2007. when the file is shared to edit other cell from the different user.

Post a Comment