Sei sulla pagina 1di 13

Excel VBA -

th 4

Session

End Sub

Sub macro1()

Declaring Variables
A 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. 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.

A Variable name can be no longer than 255 characters.


A Variable name cannot be the same as any one of Excel's key words. In other words you cannot name a Variable with such names as Sheet, Worksheet etc. 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.

List of Data Types

Byte Data Type


Byte is VBA's smallest numeric data type and holds a (natural) numeric value from 0 to 255. In this data type only positive numbers to be used. Otherwise VBA will return with an error.
Example for Byte Data Type : Sub atz() Dim Value As Byte Value = 246 End Sub
We are going discuss more about Byte and cbyte() in future lessons

Integer Data Type


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.

Example: Dim paraNo As Integer paraNo = 1

Long Data Type


This type holds long integer values, which VBA defines as whole numbers between 2,147,483,648 and 2,147,483,647. Here's an example (note that you don't include commasor periods, if you're in Europein numbers that would normally use one or more thousands separators):

Example: Dim wordCount As Long wordCount = 100000

Single Data Type


This type holds single-precision floating point values, which are numbers that have a decimal component.

Example: Dim averageUnitSales As Single averageUnitSales = 41.0

Double Data Type


This type holds double-precision floating point values, which can accommodate much larger or smaller numbers than the Single type. Note, however, that the range available with the Single type should be more than enough for your VBA macros, so you'll probably never use the Double type.

Currency Data Type


This type holds monetary values. The value range is from 922,337,203,685,477.5808 to 922,337,203,685,477.5807. To declare such a variable, use the Currency keyword

Example: Dim mySalary As Currency

String Data Type


This type holds strings, which are simple text values. Here's a sample declaration and assignment statement . (note the use of quotation marks in the assignment statement value; this tells VBA that the value is a string):

Example: Dim AFileName As String AFileName = ameetz.doc"

Variant Data Type


If you don't include a data type when declaring a variable, VBA assigns the Variant data type. This enables you to store any kind of data in the variable. However, this isn't a good idea because Variant variables use more memory and are much slower than the other data types.

Boolean Data Type


This type holds Boolean values, which take one of two values: True or False.

Example: Dim documentSaved As Boolean documentSaved = False

Date Data Type


This type holds date values, which refers to dates and/or times. Here are a few examples (note the use of the # character around the values; this tells VBA that the values are dates and/or times)

Example: Dim myBirthDate As Date Dim myBirthTime As Date myBirthDate = #8/23/59# myBirthTime = #3:02 AM#

Potrebbero piacerti anche