18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Introducing Visual Basic for Applications 39<br />

In many cases, you can refer to the same object in a number of different ways. Assume that you have a<br />

workbook named Sales.xlsx and it’s the only workbook open. Furthermore, assume that this workbook<br />

has one worksheet, named Summary. Your VBA code can refer to the Summary sheet in any of the following<br />

ways:<br />

Workbooks(“Sales.xlsx”).Worksheets(“Summary”)<br />

Workbooks(1).Worksheets(1)<br />

Workbooks(1).Sheets(1)<br />

Application.ActiveWorkbook.ActiveSheet<br />

ActiveWorkbook.ActiveSheet<br />

ActiveSheet<br />

The method that you use is determined by how much you know about the workspace. For example, if more<br />

than one workbook is open, the second or third method is not reliable. If you want to work with the active<br />

sheet (whatever it may be), any of the last three methods would work. To be absolutely sure that you’re<br />

referring to a specific sheet on a specific workbook, the first method is your best choice.<br />

Methods<br />

Objects also have methods. You can think of a method as an action taken with an object. For example,<br />

Range objects have a Clear method. The following VBA statement clears a Range, an action that is equivalent<br />

to selecting the Range and then choosing Home ➪ Editing ➪ Clear ➪ Clear All:<br />

Range(“A1:C12”).Clear<br />

In VBA code, methods look like properties because they are connected to the object with a “dot.” However,<br />

methods and properties are different concepts.<br />

Variables<br />

Like all programming languages, VBA enables you to work with variables. In VBA (unlike in some languages),<br />

you don’t need to declare variables explicitly before you use them in your code (although doing so<br />

is definitely a good practice).<br />

If your VBA module contains an Option Explicit statement at the top of the module, then you<br />

must declare all variables in the module. Undeclared variables will result in a compile error,<br />

and your procedures will not run.<br />

NOTE<br />

In the following example, the value in cell A1 on Sheet1 is assigned to a variable named Rate:<br />

rate = Worksheets(“Sheet1”).Range(“A1”).Value<br />

You then can work with the variable Rate in other parts of your VBA code. Note that the variable Rate is<br />

not a named range, which means that you can’t use it as such in a worksheet formula.<br />

Controlling execution<br />

VBA uses many constructs that are found in most other programming languages. These constructs are used to<br />

control the flow of execution. This section introduces a few of the more common programming constructs.<br />

The If-Then construct<br />

One of the most important control structures in VBA is the If-Then construct, which gives your applications<br />

decision-making capability. The basic syntax of the If-Then structure is as follows:<br />

If condition Then statements [Else elsestatements]<br />

699

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!