0% found this document useful (0 votes)
99 views

Wrox Beginning Tutorial Ebooks - Excel 2007 Vba Programmers Reference - 2

This document discusses variable and object declaration in VBA. It recommends using one-letter prefixes to indicate variable types like "i" for Integer and "d" for Double. Two-letter prefixes should indicate object types like "wb" for Workbook. Constants are declared with Const and can be Public. Object variables are assigned using Set and should match the specific object type.

Uploaded by

Hendra Suryatno
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Wrox Beginning Tutorial Ebooks - Excel 2007 Vba Programmers Reference - 2

This document discusses variable and object declaration in VBA. It recommends using one-letter prefixes to indicate variable types like "i" for Integer and "d" for Double. Two-letter prefixes should indicate object types like "wb" for Workbook. Constants are declared with Const and can be Public. Object variables are assigned using Set and should match the specific object type.

Uploaded by

Hendra Suryatno
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter 1: Primer in Excel VBA

Many users assume that this declares each variable to be Integer. This is not true. Coland Roware Variantbecause they have not been given a type. To declare all three as Integer, the line should be as follows: Dim Col As Integer, Row As Integer, Sheet As Integer

Declaring Function and Parameter Types


If you have input parameters for sub procedures or function procedures, you can define each parameter type in the first line of the procedure as follows: Function IsHoliday(WhichDay As Date) Sub Marine(CrewSize As Integer, FuelCapacity As Double) You can also declare the return value type for a function. The following example is for a function that returns a value of Trueor False: Function IsHoliday(WhichDay As Date) As Boolean

Constants
You have seen that many intrinsic constants are built into VBA, such as vbYesand vbNo,discussed previously. You can also define your own constants. Constants are handy for holding numbers or pieces of text that do not change while your code is running, but that you want to use repeatedly in calculations and messages. Constants are declared using the Constkeyword, as follows: Const Pi = 3.14159265358979 You can include the constants type in the declaration: Const Version As String = Release 3.9a Constants follow the same rules regarding scope as variables. If you declare a constant within a procedure, it will be local to that procedure. If you declare it in the declarations section of a module, it will be available to all procedures in the module. If you want to make it available to all modules, you can declare it to be Publicas follows: Public Const Error666 As String = You cant do that

Variable Naming Conventions


You can call your variables and user-defined functions anything you want, except where there is a clash with VBA keywords and function names. However, many programmers adopt a system whereby the variable or object type is included, in abbreviated form, in the variable name, usually as a prefix, so instead of declaring: Dim SalesData As Double you can use: Dim dSalesData As Double

44

Chapter 1: Primer in Excel VBA


Wherever dSalesDataappears in your code, you will be reminded that the variable is of type Double. Alternatively, you could use this line of code: Dim dblSalesData As Double For the sake of simplicity, this approach has not been used so far in this chapter, but from here onward, the examples will use a system to create variable names. This is the convention used in this book: One-letter prefixes for the common data types: Dim iColumn As Integer Dim lRow As Long Dim dProduct As Double Dim sName As String Dim vValue As Variant Dim bChoice As Boolean Two- or three-letter prefixes for object types: Dim objExcel As Object Dim rngData As Range Dim wkbSales As Workbook In addition to these characters, a lowercase awill be inserted in front of array variables, which are discussed later in this chapter. If the variable is a module-level variable, it will also have a lowercase m placed in front of it. If it is a public variable, it will have a lowercase g(for global) placed in front of it. For example, malEffectwould be a module-level array variable containing long integer values.

Object Variables
The variables you have seen so far have held data such as numbers and text. You can also create object variables to refer to objects such as worksheets and ranges. The Setstatement is used to assign an object reference to an object variable. Object variables should also be declared and assigned a type as with normal variables. If you dont know the type, you can use the generic term Objectas the type: Dim objWorkbook As

Object Set objWorkbook = ThisWorkbook MsgBox objWorkbook.Name It is more efficient to use the specific object type if you can. The following code creates an object variable rng,referring to cell B10 in Sheet1, in the same workbook as the code. It then assigns values to the object and the cell above: Sub ObjectVariable() Dim rng As Range Set rng = ThisWorkbook.Worksheets(Sheet1).Range(C10) rng.Value = InputBox(Enter Sales for January) rng.Offset(1, 0).Value = January Sales End Sub If you are going to refer to the same object more than once, it is more efficient to create an object variable than to keep repeating a lengthy specification of the object. It also makes code easier to read and write.

45

You might also like