1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Quản trị mạng >

Figure 19-24. A conditionally formatted range

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.72 MB, 490 trang )

.FormatConditions.Add xlExpression, , _

Range("A1").Value = Range("A2").Value

.FormatConditions(1).Interior.Color = _

RGB(0, 0, 255)

.FormatConditions.Add xlExpression, , _

Range("A1").Value <> Range("A2").Value

.FormatConditions(2).Interior.Color = _

RGB(255, 0, 0)

End With

As the previous examples show, the actual formatting is done by setting some of the properties of

children of the FormatCondition object. In particular, the Borders, Font, and Interior properties

return child objects of the same name, whose properties can be set to indicate the desired


Note finally that an existing FormatCondition object can be deleted using the Delete method of the

FormatConditions collection, and it can be changed using the Modify method of the

FormatCondition object. The Modify method has the syntax:

FormatConditionObject.Modify(Type, Operator, Formula1, Formula2)

where the parameters are identical to those of the Add method.

19.4.8 The Interior Object

The Interior object represents the characteristics of the interior region of a cell (or range of cells).

The Interior object has only a handful of properties (and no methods), as described in this section. Color and ColorIndex properties

These properties are analogous to the properties by the same name of the Borders object,

discussed earlier. They set the interior of a cell (or cells) to the color specified. Pattern property

This property returns or sets the interior pattern. It can be one of the following XlPattern


Enum XlPattern

xlPatternVertical = -4166

xlPatternUp = -4162

xlPatternNone = -4142

xlPatternHorizontal = -4128

xlPatternGray75 = -4126

xlPatternGray50 = -4125

xlPatternGray25 = -4124

xlPatternDown = -4121

xlPatternAutomatic = -4105

xlPatternSolid = 1

xlPatternChecker = 9

xlPatternSemiGray75 = 10

xlPatternLightHorizontal = 11

xlPatternLightVertical = 12

xlPatternLightDown = 13

xlPatternLightUp = 14

xlPatternGrid = 15

xlPatternCrissCross = 16


xlPatternGray16 = 17

xlPatternGray8 = 18

End Enum

Note that this provides another way to access grayscales. PatternColor and PatternColorIndex properties

These properties set the color (or color index) of the pattern used to fill the interior of a cell. For

more on setting color and color indexes, please see the discussion of the Color and ColorIndex

properties of the Border object.

19.4.9 The PivotField, PivotItem, and PivotTable Objects

These objects relate to PivotTable objects and will be discussed in Chapter 20.

19.4.10 The QueryTable Object

A QueryTable object represents a worksheet table that is built from data returned from an external

data source, such as Microsoft SQL Server or a Microsoft Access database. We will not discuss

QueryTable objects in this book. (There are better ways to retrieve data from an external source.)

19.4.11 The Validation Object

A Validation object is used to enforce data validation on a cell or range of cells. The Validation

property of the Range object returns a Validation object, whose properties can be returned or set.

Note that there is no Validations collection.

Data validation involves three parts: the actual validation, an input message that can be displayed

when a cell is activated, and an error message that can be displayed if the data entered is invalid.

The methods of the Validation object are Add, Delete, and Modify. To add validation to a range,

use the Add method, whose syntax is:

ValidationObject.Add(Type, AlertStyle, Operator, Formula1, Formula2)

Note the similarity between the parameters of the Add method of the Validation object and the

Add method of the FormatConditions object.

The required Type parameter specifies the type of data allowed and can be one of the following

XlDVType constants:

Enum XlDVType

xlValidateInputOnly = 0

xlValidateWholeNumber = 1

xlValidateDecimal = 2

xlValidateList = 3

xlValidateDate = 4

xlValidateTime = 5

xlValidateTextLength = 6

xlValidateCustom = 7

End Enum


The xlValidateInputOnly constant causes Excel to treat all data as valid. This value should

be used when we want to display an input message (described later in this section), but not invoke

data validation.

The optional AlertStyle parameter specifies the buttons that will appear on the error dialog

box that is displayed if the data entered is invalid. It can be one of the following

XlDVAlertStyle constants:

Enum XlDVAlertStyle

xlValidAlertStop = 1

xlValidAlertWarning = 2

xlValidAlertInformation = 3

End Enum

The meanings of these constants are as follows:


OK and Cancel buttons


Retry and Cancel buttons


Yes, No, and Cancel buttons

The optional Operator parameter is the operator used in the validation, and can be any one of

the XlFormatConditionOperator constants:

Enum XlFormatConditionOperator

xlBetween = 1

xlNotBetween = 2

xlEqual = 3

xlNotEqual = 4

xlGreater = 5

xlLess = 6

xlGreaterEqual = 7

xlLessEqual = 8

End Enum

The Formula1 parameter specifies the first part of the data-validation equation and Formula2

specifies the second part when Operator is xlBetween or xlNotBetween.

To understand this rather complex object, it is best to look at the corresponding dialog boxes in the

Excel user interface. Figure 19-25 shows the Settings tab of the Validation dialog box.

Figure 19-25. The Settings tab of the Data Validation dialog


This dialog corresponds to setting:








IgnoreBlank = True


You can learn more about the Type constants by clicking on the ? button in the Data Validation

dialog and then clicking on the Allow drop-down list box. Note that the other controls on the tab

in Figure 19-25 will change depending upon the value selected in the Allow drop-down box.

The Input Message tab is shown in Figure 19-26. The values in this dialog correspond to

properties of the Validation object. In particular, we have:

ShowInput = True

InputTitle = "Input:"

InputMessage = "Input a number"

Figure 19-26. The Input Message tab of the Data Validation dialog

277 ®


Xem Thêm
Tải bản đầy đủ (.pdf) (490 trang)