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.

62<br />

Part II: How VBA Works with Excel<br />

Object properties<br />

Every object has properties. You can think of properties as attributes that<br />

describe the object. An object’s properties determine how it looks, how it<br />

behaves, and even whether it is visible. Using VBA, you can do two things<br />

with an object’s properties:<br />

✓ Examine the current setting for a property.<br />

✓ Change the property’s setting.<br />

For example, a single-cell Range object has a property called Value. The<br />

Value property stores the value contained in the cell. You can write VBA<br />

code to display the Value property, or you may write VBA code to set the<br />

Value property to a specific value. The following macro uses the VBA builtin<br />

MsgBox function to bring up a box that displays the value in cell A1 on<br />

Sheet1 of the active workbook. See Figure 4-2.<br />

Sub ShowValue()<br />

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

MsgBox Contents<br />

End Sub<br />

Figure 4-2:<br />

This message<br />

box<br />

displays<br />

a Range<br />

object’s<br />

Value<br />

property.<br />

By the way, MsgBox is a very useful function. You can use it to display results<br />

while Excel executes your VBA code. I tell you more about this function in<br />

Chapter 15, so be patient (or just skip and read all about it).<br />

The code in the preceding example displays the current setting of a cell’s<br />

Value property. What if you want to change the setting for that property?<br />

The following macro changes the value in cell A1 by changing the cell’s Value<br />

property:<br />

Sub ChangeValue()<br />

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

End Sub

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

Saved successfully!

Ooh no, something went wrong!