30.04.2017 Views

4523756273

Create successful ePaper yourself

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

114<br />

Part III: Programming Concepts<br />

dynamic array. It assumes that the NumElements variable contains a value,<br />

which your code calculated.<br />

ReDim MyArray (1 To NumElements)<br />

When you redimension an array by using ReDim, you wipe out any values currently<br />

stored in the array elements. You can avoid destroying the old values<br />

by using the Preserve keyword. The following example shows how you can<br />

preserve an array’s values when you redimension the array:<br />

ReDim Preserve MyArray(1 To NumElements)<br />

If MyArray currently has ten elements and you execute the preceding statement<br />

with NumElements equaling 12, the first ten elements remain intact, and<br />

the array has room for two additional elements (up to the number contained<br />

in the variable NumElements). If NumElements equals 7 however, the first<br />

seven elements are retained but the remaining three elements meet their<br />

demise.<br />

The topic of arrays comes up again in Chapter 10, when I discuss looping.<br />

Using Labels<br />

In early versions of BASIC, every line of code required a line number. For<br />

example, if you had written a BASIC program in the ’70s (dressed, of course,<br />

in your bell bottoms), it may have looked something like this:<br />

010: LET X=5<br />

020: LET Y=3<br />

030: LET Z=X*Y<br />

040: PRINT Z<br />

050: END<br />

VBA permits the use of such line numbers, and it even permits text labels. You<br />

don’t typically use a label for each line, but you may occasionally need to use<br />

a label. For example, insert a label if you use a GoTo statement (which I discuss<br />

in Chapter 10). A label must begin with the first nonblank character in a<br />

line and end with a colon.<br />

The information in this chapter becomes clearer as you read subsequent<br />

chapters. If you want to find out more about VBA language elements, I refer<br />

you to the VBA Help system. You can find as much detail as you need, or<br />

care, to know.

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

Saved successfully!

Ooh no, something went wrong!