30.04.2017 Views

4523756273

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

124<br />

Part III: Programming Concepts<br />

For example, the following statement changes the Color property of the<br />

Interior object contained in the Range object:<br />

Range(“A1”).Interior.Color = 8421504<br />

In other words, this statement changes the cell’s background to middle gray.<br />

What’s that? You didn’t know that 8421504 is middle gray? For some insights<br />

into Excel’s wonderful world of color, see the sidebar, “A quick & dirty color<br />

primer.”<br />

The Formula property<br />

The Formula property represents the formula in a cell. This is a read-write<br />

property, so you can access it to insert a formula into a cell. For example, the<br />

following statement enters a SUM formula into cell A13:<br />

Range(“A13”).Formula = “=SUM(A1:A12)”<br />

Notice that the formula is a text string and is enclosed in quotation marks.<br />

If the formula itself contains quotation marks, things get a bit tricky. For<br />

example, let’s say you want to insert this formula by using VBA:<br />

=SUM(A1:A12)&” Stores”<br />

This formula displays a value, followed by the word Stores. To make this formula<br />

acceptable, you need to replace every quotation mark in the formula<br />

with two quotation marks. Otherwise, VBA will get confused, and claim that<br />

there’s a syntax error (because there is!). So here’s a statement that will<br />

enter a formula that contains quotes:<br />

Range(“A13”).Formula = “=SUM(A1:A12)&”” Stores”””<br />

By the way, you can access a cell’s Formula property even if the cell doesn’t<br />

have a formula. If a cell has no formula, the Formula property returns the<br />

same as its Value property.<br />

If you need to know whether a cell has a formula, use the HasFormula<br />

property.<br />

Be aware that VBA “speaks” U.S. English. This means that in order to put a<br />

formula in a cell, you must use the U.S. syntax. To use your own locale formula<br />

syntax in VBA, check the FormulaLocal property.

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

Saved successfully!

Ooh no, something went wrong!