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 />

VBA Coding Tips<br />

When you enter code in a module sheet, you’re free to use indenting and blank lines to make the code<br />

more readable. (In fact, this is an excellent habit.)<br />

After you enter a line of code (by pressing Enter), it’s evaluated for syntax errors. If none are found, the line of<br />

code is reformatted, and colors are added to keywords and identifiers. This automatic reformatting adds consistent<br />

spaces (before and after an equal sign, for example) and removes extra spaces that aren’t needed. If a<br />

syntax error is found, you receive a pop-up message, and the line is displayed in a different color (red, by<br />

default). You need to correct your error before you can execute the macro.<br />

A single statement can be as long as you need. However, you may want to break the statement into two or<br />

more lines. To do so, insert a space followed by an underscore(_). The following code, although written as two<br />

lines, is actually a single VBA statement:<br />

Sheets(“Sheet1”).Range(“B1”).Value = _<br />

Sheets(“Sheet1”).Range(“A1”).Value<br />

You can insert comments freely into your VBA code. The comment indicator is an apostrophe single quote<br />

character (‘). Any text that follows a single quote is ignored. A comment can be a line by itself, or it can be<br />

inserted after a statement. The following examples show two comments:<br />

‘ Assign the values to the variables<br />

Rate = .085 ‘Rate as of November 16<br />

2. The Project window displays a list of all open workbooks and add-ins. Locate the workbook<br />

that you’re currently working in and select it.<br />

3. Choose Insert ➪ Module. VBA inserts a new (empty) module into the workbook and displays it<br />

in the Code window.<br />

A VBA module, which is displayed in a separate window, works like a text editor. You can move through the<br />

sheet, select text, insert, copy, cut, paste, and so on.<br />

How VBA works<br />

VBA is by far the most complex feature in Excel, and you can easily get overwhelmed. To set the stage for<br />

the details of VBA, here is a concise summary of how VBA works:<br />

n<br />

n<br />

You perform actions in VBA by writing (or recording) code in a VBA module sheet and then<br />

executing the macro in any one of various ways. VBA modules are stored in an Excel workbook,<br />

and a workbook can hold any number of VBA modules. To view or edit a VBA module, you<br />

must activate the Visual Basic Editor window. (Press Alt+F11 to toggle between Excel and the VB<br />

Editor window.)<br />

A VBA module consists of procedures. A procedure is basically computer code that performs<br />

some action. The following is an example of a simple Sub procedure called ShowSum (it adds 1 +<br />

1 and displays the result):<br />

Sub ShowSum()<br />

Sum = 1 + 1<br />

MsgBox “The answer is “ & Sum<br />

End Sub<br />

695

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

Saved successfully!

Ooh no, something went wrong!