Read-only End Returns a Range object that represents the cell at the end of the region that contains the source range.. EntireColumn Returns a Range object that represents the entire col
Trang 1THUỘC TÍNH CỦA ĐỐI TƯỢNG RANGE
Properties
AddIndent Returns or sets a Variant value that indicates if text is automatically indented when the text
alignment in a cell is set to equal distribution (either horizontally or vertically.) Address Returns a String value that represents the range reference in the language of the macro AddressLocal Returns the range reference for the specified range in the language of the user Read-only
Areas Returns an Areas collection that represents all the ranges in a multiple-area selection
Read-only
Borders Returns a Borders collection that represents the borders of a style or a range of cells
(including a range defined as part of a conditional format)
Cells Returns a Range object that represents the cells in the specified range
Characters Returns a Characters object that represents a range of characters within the object text
You can use the Characters object to format characters within a text string
Column Returns the number of the first column in the first area in the specified range Read-only
Long
Columns Returns a Range object that represents the columns in the specified range
ColumnWidth Returns or sets the width of all columns in the specified range Read/write Variant
Comment Returns a Comment object that represents the comment associated with the cell in the
upper-left corner of the range
Count Returns a Long value that represents the number of objects in the collection
CountLarge Returns a value that represents the number of objects in the collection Read-only Variant Creator Returns a 32-bit integer that indicates the application in which this object was created
Read-only Long
CurrentArray If the specified cell is part of an array, returns a Range object that represents the entire
array Read-only
CurrentRegion Returns a Range object that represents the current region The current region is a range
bounded by any combination of blank rows and blank columns Read-only
Dependents
Returns a Range object that represents the range containing all the dependents of a cell This can be a multiple selection (a union of Range objects) if there’s more than one dependent Read-only Range object
DirectDependents
Returns a Range object that represents the range containing all the direct dependents of a cell This can be a multiple selection (a union of Range objects) if there’s more than one dependent Read-only Range object
DirectPrecedents
Returns a Range object that represents the range containing all the direct precedents of a cell This can be a multiple selection (a union of Range objects) if there’s more than one precedent Read-only Range object
DisplayFormat Returns a DisplayFormat object that represents the display settings for the specified range
Read-only End
Returns a Range object that represents the cell at the end of the region that contains the source range Equivalent to pressing END+UP ARROW, END+DOWN ARROW, END+LEFT ARROW, or END+RIGHT ARROW Read-only Range object
EntireColumn Returns a Range object that represents the entire column (or columns) that contains the
specified range Read-only
Trang 2EntireRow Returns a Range object that represents the entire row (or rows) that contains the specified
range Read-only
Errors Allows the user to to access error checking options
Font Returns a Font object that represents the font of the specified object
FormatConditions Returns a FormatConditions collection that represents all the conditional formats for the
specified range Read-only
Formula Returns or sets a Variant value that represents the object's formula in A1-style notation and
in the macro language
FormulaArray
Returns or sets the array formula of a range Returns (or can be set to) a single formula or a Visual Basic array If the specified range doesn't contain an array formula, this property returns null Read/write Variant
FormulaHidden Returns or sets a Variant value that indicates if the formula will be hidden when the
worksheet is protected
FormulaLocal Returns or sets the formula for the object, using A1-style references in the language of the
user Read/write Variant
FormulaR1C1 Returns or sets the formula for the object, using R1C1-style notation in the language of the
macro Read/write Variant
FormulaR1C1Local Returns or sets the formula for the object, using R1C1-style notation in the language of the
user Read/write Variant
HasArray True if the specified cell is part of an array formula Read-only Variant
HasFormula True if all cells in the range contain formulas; False if none of the cells in the range
contains a formula; null otherwise Read-only Variant
Height Returns or sets a Variant value that represents the height, in points, of the range
Hidden Returns or sets a Variant value that indicates if the rows or columns are hidden
HorizontalAlignment Returns or sets a Variant value that represents the horizontal alignment for the specified
object
Hyperlinks Returns a Hyperlinks collection that represents the hyperlinks for the range
ID Returns or sets a String value that represents the identifying label for the specified cell
when the page is saved as a Web page
IndentLevel Returns or sets a Variant value that represents the indent level for the cell or range Can be
an integer from 0 to 15
Interior Returns an Interior object that represents the interior of the specified object
Item Returns a Range object that represents a range at an offset to the specified range
Left Returns a Variant value that represents the distance, in points, from the left edge of column
A to the left edge of the range
ListHeaderRows Returns the number of header rows for the specified range Read-only Long
ListObject Returns a ListObject object for the Range object Read-only ListObject object
LocationInTable
Returns a constant that describes the part of the PivotTable report that contains the left corner of the specified range Can be one of the following XlLocationInTable
upper-constants Read-only Long
Locked Returns or sets a Variant value that indicates if the object is locked
MDX Returns the MDX name for the specified Range object Read-only String
MergeArea
Returns a Range object that represents the merged range containing the specified cell If the specified cell isn’t in a merged range, this property returns the specified cell Read-only Variant
MergeCells True if the range contains merged cells Read/write Variant
Name Returns or sets a Variant value that represents the name of the object
Next Returns a Range object that represents the next cell
NumberFormat Returns or sets a Variant value that represents the format code for the object
NumberFormatLocal Returns or sets a Variant value that represents the format code for the object as a string in
the language of the user
Trang 3Offset Returns a Range object that represents a range that’s offset from the specified range
Orientation Returns or sets a Variant value that represents the text orientation
OutlineLevel Returns or sets the current outline level of the specified row or column Read/write Variant PageBreak
Returns or sets the location of a page break Can be one of the following XlPageBreak constants: xlPageBreakAutomatic, xlPageBreakManual, or xlPageBreakNone
Read/write Long
Parent Returns the parent object for the specified object Read-only
Phonetic Returns the Phonetic object, which contains information about a specific phonetic text
string in a cell
Phonetics Returns the Phonetics collection of the range Read only
PivotCell Returns a PivotCell object that represents a cell in a PivotTable report
PivotField Returns a PivotField object that represents the PivotTable field containing the upper-left
corner of the specified range
PivotItem Returns a PivotItem object that represents the PivotTable item containing the upper-left
corner of the specified range
PivotTable Returns a PivotTable object that represents the PivotTable report containing the upper-left
corner of the specified range
Precedents Returns a Range object that represents all the precedents of a cell This can be a multiple
selection (a union of Range objects) if there's more than one precedent Read-only
PrefixCharacter Returns the prefix character for the cell Read-only Variant
Previous Returns a Range object that represents the next cell
QueryTable Returns a QueryTable object that represents the query table that intersects the specified
Range object
Range Returns a Range object that represents a cell or a range of cells
ReadingOrder Returns or sets the reading order for the specified object Can be one of the following
constants: xlRTL (right-to-left), xlLTR (left-to-right), or xlContext Read/write Long Resize Resizes the specified range Returns a Range object that represents the resized range Row Returns the number of the first row of the first area in the range Read-only Long
RowHeight Returns or sets the height of the first row in the range specified, measured in points
ShrinkToFit Returns or sets a Variant value that indicates if text automatically shrinks to fit in the
available column width
SoundNote This property should not be used Sound notes have been removed from Microsoft Excel SparklineGroups Returns a SparklineGroups object that represents an existing group of sparklines from the
specified range Read-only Style Returns or sets a Variant value, containing a Style object, that represents the style of the
specified range
Summary True if the range is an outlining summary row or column The range should be a row or a
column Read-only Variant
Text Returns or sets the text for the specified object Read-only String
Top Returns a Variant value that represents the distance, in points, from the top edge of row 1
to the top edge of the range
UseStandardHeight True if the row height of the Range object equals the standard height of the sheet Returns
Null if the range contains more than one row and the rows aren’t all the same height
Trang 4Read/write Variant
UseStandardWidth
True if the column width of the Range object equals the standard width of the sheet
Returns null if the range contains more than one column and the columns aren’t all the same width Read/write Variant
Validation Returns the Validation object that represents data validation for the specified range
Read-only
Value Returns or sets a Variant value that represents the value of the specified range
Value2 Returns or sets the cell value Read/write Variant
VerticalAlignment Returns or sets a Variant value that represents the vertical alignment of the specified object Width Returns a Variant value that represents the width, in units, of the range
Worksheet Returns a Worksheet object that represents the worksheet containing the specified range
Range.AddIndent Property (Excel)
expression Address(RowAbsolute, ColumnAbsolute, ReferenceStyle, External, RelativeTo)
expression A variable that represents a Range object
Parameters
RowAbsolute Optional Variant True to return the row part of the reference as an
absolute reference The default value is True
ColumnAbsolute Optional Variant True to return the column part of the reference as an
absolute reference The default value is True
ReferenceStyle Optional XlReferenceStyle The reference style The default value is xlA1
External Optional Variant True to return an external reference False to return a
local reference The default value is False
RelativeTo Optional Variant
If RowAbsolute and ColumnAbsolute are False, and ReferenceStyle is xlR1C1, you must include a starting point for the relative reference This argument is a Range object that defines the starting point
Trang 5Remarks
If the reference contains more than one cell, RowAbsolute and ColumnAbsolute apply to all rows and columns Example
The following example displays four different representations of the same cell address on Sheet1 The comments
in the example are the addresses that will be displayed in the message boxes
VBA
Set mc = Worksheets("Sheet1").Cells(1, 1)
MsgBox mc.Address() ' $A$1
MsgBox mc.Address(RowAbsolute:=False) ' $A1
expression AddressLocal(RowAbsolute, ColumnAbsolute, ReferenceStyle, External, RelativeTo)
expression A variable that represents a Range object
Parameters
RowAbsolute Optional Variant True to return the row part of the reference as an
absolute reference The default value is True
ColumnAbsolute Optional Variant True to return the column part of the reference as an
absolute reference The default value is True
ReferenceStyle Optional XlReferenceStyle One of the constants for XlReferenceStyle specifying
A1-style or R1C1-style reference
External Optional Variant True to return an external reference False to return a
local reference The default value is False
RelativeTo Optional Variant
If RowAbsolute and ColumnAbsolute are both set to False and ReferenceStyle is set to xlR1C1, you must include a starting point for the relative reference This argument is a Range object that defines the starting point for the reference
MsgBox mc.AddressLocal() ' $A$1
MsgBox mc.AddressLocal(RowAbsolute:=False) ' $A1
Trang 6' Notify the user about editing cell A1
If wksOne.Range("A1").AllowEdit = True Then
MsgBox "Cell A1 can be edited."
Set myObject = ActiveWorkbook
If myObject.Application.Value = "Microsoft Excel" Then
MsgBox "This is an Excel Application object."
Trang 7Example
This example sets the font style for cells A1:C5 on Sheet1 to italic
VBA
Worksheets("Sheet1").Activate
Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True
This example scans a column of data named "myRange." If a cell has the same value as the cell immediately above it, the example displays the address of the cell that contains the duplicate data
VBA
Set r = Range("myRange")
For n = 1 To r.Rows.Count
If r.Cells(n, 1) = r.Cells(n + 1, 1) Then
MsgBox "Duplicate data in " & r.Cells(n + 1, 1).Address
End If
Next n
Sample code provided by: Holy Macro! Books, Holy Macro! It’s 2,500 Excel VBA Examples | About the
Contributor
This example looks through column C, and for every cell that has a comment, it puts the comment text into
column D and deletes the comment from column C
For iRow = 1 To WorksheetFunction.CountA(Columns(3))
Set cmt = Cells(iRow, 3).Comment
If Not cmt Is Nothing Then
expression Characters(Start, Length)
expression A variable that represents a Range object
Parameters
Name Required/Optional Data
Start Optional Variant The first character to be returned If this argument is either 1 or omitted,
this property returns a range of characters starting with the first character Length Optional Variant The number of characters to be returned If this argument is omitted, this
Trang 8property returns the remainder of the string (everything after the Start character)
Column A returns 1, column B returns 2, and so on
To return the number of the last column in the range, use the following expression
myRange.Columns(myRange.Columns.Count).Column
Example
This example sets the column width of every other column on Sheet1 to 4 points
VBA
For Each col In Worksheets("Sheet1").Columns
If col.Column Mod 2 = 0 Then
col.ColumnWidth = 4
End If
Next col
Sample code provided by: Dennis Wallentin, VSTO & NET & Excel | About the Contributor
This example deletes the empty columns from a selected range
VBA
Sub Delete_Empty_Columns()
'The range from which to delete the columns
Dim rnSelection As Range
'Column and count variables used in the deletion process
Dim lnLastColumn As Long
Dim lnColumnCount As Long
Dim lnDeletedColumns As Long
lnDeletedColumns = 0
'Confirm that a range is selected, and that the range is contiguous
If TypeName(Selection) = "Range" Then
If Selection.Areas.Count = 1 Then
'Initialize the range to what the user has selected, and
initialize the count for the upcoming FOR loop
Set rnSelection = Application.Selection
MsgBox "Please select only one area.", vbInformation
Trang 9Using this property without an object qualifier is equivalent to using ActiveSheet.Columns
When applied to a Range object that's a multiple-area selection, this property returns columns from only the first area of the range For example, if the Range object has two areas — A1:B2 and C3:D4 —
Selection.Columns.Count returns 2, not 4 To use this property on a range that may contain a multiple-area selection, test Areas.Count to determine whether the range contains more than one area If it does, loop over each area in the range
MsgBox "The selection contains " & _
Selection.Columns.Count & " columns."
Else
For i = 1 To areaCount
MsgBox "Area " & i & " of the selection contains " & _
Selection.Areas(i).Columns.Count & " columns."
Use the Width property to return the width of a column in points
If all columns in the range have the same width, the ColumnWidth property returns the width If columns in the range have different widths, this property returns null
Trang 10Range.Count Property (Excel)
MsgBox "Area " & i & " of the selection contains " & _
Selection.Areas(i).Columns.Count & " columns."
Range.Creator Property (Excel)
Trang 12Note that the DisplayFormat property does not work in user defined functions For example, in a worksheet function that returns the interior color of a cell, you use a line similar to:
Range(n).DisplayFormat.Interior.ColorIndex When the worksheet function executes, it returns a #VALUE! error
In another example, you cannot use the DisplayFormat property in a worksheet function to return settings for a particular range DisplayFormat will work in a function called from Visual Basic for Applications (VBA), however For instance, in the following function:
VBA
Function getColorIndex()
getColorIndex = ActiveCell.DisplayFormat.Interior.ColorIndex
End Function
Calling the function from a worksheet as follows =getColorIndex() returns the #VALUE! error
However, when the function is called from the Immediate pane in the Visual Basic Editor, it returns the color index for the active cell To work around this issue, remove DisplayFormat from the code The following
function will work either from a worksheet or from VBA:
Name Required/Optional Data Type Description
Direction Required XlDirection The direction in which to move
Trang 13Sub SortAll()
'Turn off screen updating, and define your variables
Application.ScreenUpdating = False
Dim lngLastRow As Long, lngRow As Long
Dim rngHidden As Range
'Determine the number of rows in your sheet, and add the header row to the hidden range variable
lngLastRow = Cells(Rows.Count, 1).End(xlUp).Row
Set rngHidden = Rows(1)
'For each row in the list, if the row is hidden add that that row to the hidden range variable
For lngRow = 1 To lngLastRow
If Rows(lngRow).Hidden = True Then
Set rngHidden = Union(rngHidden, Rows(lngRow))
'Turn screen updating back on
Set rngHidden = Nothing
If Range("A1").Errors.Item(xlNumberAsText).Value = True Then
MsgBox "The number is written as text."
Trang 14' Determine if the font name for selected cell is Arial
If Range("A1").Font.Name = "Arial" Then
MsgBox "The font name for this cell is 'Arial'"
Modify xlCellValue, xlLess, "=$a$1"
Range.Formula Property (Excel)
Syntax
expression Formula
expression A variable that represents a Range object
Remarks
This property is not available for OLAP data sources
If the cell contains a constant, this property returns the constant If the cell is empty, this property returns an empty string If the cell contains a formula, the Formula property returns the formula as a string in the same format that would be displayed in the formula bar (including the equal sign (=))
If you set the value or formula of a cell to a date, Microsoft Excel verifies that cell is already formatted with one
of the date or time number formats If not, Microsoft Excel changes the number format to the default short date number format
If the range is a one- or two-dimensional range, you can set the formula to a Visual Basic array of the same dimensions Similarly, you can put the formula into a Visual Basic array
Setting the formula for a multiple-cell range fills all cells in the range with the formula
Example
The following code example sets the formula for cell A1 on Sheet1
VBA
Worksheets("Sheet1").Range("A1").Formula = "=$A$4+$A$10"
Sample code provided by: Bill Jelen, MrExcel.com | About the Contributor
The following code example sets the formula for cell A1 on Sheet1 to display today's date
Trang 15Range.FormulaArray Property (Excel)
Don’t confuse this property with the Hidden property The formula will not be hidden if the workbook is
protected and the worksheet is not, but only if the worksheet is protected
If you set the value or formula of a cell to a date, Microsoft Excel checks to see whether that cell is already formatted with one of the date or time number formats If not, the number format is changed to the default short date number format
If the range is a one- or two-dimensional range, you can set the formula to a Visual Basic array of the same dimensions Similarly, you can put the formula into a Visual Basic array
Setting the formula of a multiple-cell range fills all cells in the range with the formula
Example
Assume that you enter the formula =SUM(A1:A10) in cell A11 on worksheet one, using the American English version of Microsoft Excel If you then open the workbook on a computer that's running the German version and run the following example, the example displays the formula =SUMME(A1:A10) in a message box
Trang 16Remarks
If the cell contains a constant, this property returns the constant If the cell is empty, the property returns an empty string If the cell contains a formula, the property returns the formula as a string, in the same format in which it would be displayed in the formula bar (including the equal sign)
If you set the value or formula of a cell to a date, Microsoft Excel checks to see whether that cell is already formatted with one of the date or time number formats If not, the number format is changed to the default short date number format
If the range is a one- or two-dimensional range, you can set the formula to a Visual Basic array of the same dimensions Similarly, you can put the formula into a Visual Basic array
Setting the formula of a multiple-cell range fills all cells in the range with the formula
If you set the value or formula of a cell to a date, Microsoft Excel checks to see whether that cell is already formatted with one of the date or time number formats If not, the number format is changed to the default short date number format
If the range is a one- or two-dimensional range, you can set the formula to a Visual Basic array of the same dimensions Similarly, you can put the formula into a Visual Basic array
Setting the formula of a multiple-cell range fills all cells in the range with the formula
Example
Assume that you enter the formula =SUM(A1:A10) in cell A11 on worksheet one, using the American English version of Microsoft Excel If you then open the workbook on a computer that's running the German version and run the following example, the example displays the formula =SUMME(Z1S1:Z10S1) in a message box
If ActiveCell.HasArray =True Then
MsgBox "The active cell is part of an array"
End If
Range.Height Property (Excel)
Syntax
expression Height
expression A variable that represents a Range object
Range.Hidden Property (Excel)
Syntax
expression Hidden
expression A variable that represents a Range object
Remarks