Range 对象 (Excel) 您所在的位置:网站首页 array_range Range 对象 (Excel)

Range 对象 (Excel)

#Range 对象 (Excel) | 来源: 网络整理| 查看: 265

Range 对象 (Excel) 项目 04/07/2023

表示一个单元格、一行、一列、一个包含单个或若干连续单元格区域的选定单元格范围,或者一个三维区域。

注意

有兴趣开发跨多个平台扩展 Office 体验的解决方案吗? 查看新的 Office 外接程序模型。 与VSTO外接程序和解决方案相比,Office外接程序占用的空间较小,您可以使用几乎任何Web编程技术(例如HTML5,JavaScript,CSS3和XML)来构建它们。

说明

Range 的默认成员将不包含参数的调用转发至 Value 属性,并将包含参数的调用转发至 Item 成员。 相应地,someRange = someOtherRange 等效于 someRange.Value = someOtherRange.Value,someRange(1) 等效于 someRange.Item(1),someRange(1,1) 等效于 someRange.Item(1,1)。

有关返回 Range 对象的下述属性和方法,可查看示例部分:

Worksheet 对象的 Range 和 Cells 属性 Range 对象的 Range 和 Cells 属性 Worksheet 对象的 Rows 和 Columns 属性 Range 对象的 Rows和 Columns 属性 Range 对象的 Offset 属性 Application 对象的 Union 方法 示例

使用 Range (arg) 可返回一个 Range 对象,它表示单个单元格或单元格区域;其中 arg 对范围进行命令。 下例将单元格 A1 的值赋给单元格 A5。

Worksheets("Sheet1").Range("A5").Value = _ Worksheets("Sheet1").Range("A1").Value

下例通过为区域中的每个单元格设置公式,在区域 A1:H8 中填充随机数。 在没有对象限定符的情况下使用它时(对象位于句点左侧),Range 属性将返回活动工作表上的一个区域。 如果活动工作表不是一个工作表,则该方法将失败。

使用 Worksheet 对象的 Activate 方法在你使用 Range 属性之前激活工作表,而不使用显式对象限定符。

Worksheets("Sheet1").Activate Range("A1:H8").Formula = "=Rand()" 'Range is on the active sheet

下例清除区域名为“Criteria”的区域中的内容。

注意

如果使用文本参数指定区域地址,则必须以 A1 样式记号指定该地址(不能用 R1C1 样式记号)。

Worksheets(1).Range("Criteria").ClearContents

使用工作表上的 Cells 获取由工作表上的所有单个单元格组成的区域。 可以通过 Item(row, column) 访问单个单元格,其中,row 为行索引,column 为列索引。 Item 可以省略,因为 Range 的默认成员会将调用转发给它。 以下示例设置活动工作簿第一个工作簿上的单元格 A1 至 24 和单元格 B1 至 42 的值。

Worksheets(1).Cells(1, 1).Value = 24 Worksheets(1).Cells.Item(1, 2).Value = 42

下例设置单元格 A2 的公式。

ActiveSheet.Cells(2, 1).Formula = "=Sum(B1:B5)"

虽然也可使用 Range("A1") 返回单元格 A1,但 Cells 属性有时候更方便,因为可将变量用于行或列。 下例在 Sheet1 上创建列和行标题。 请注意,在激活工作表之后,可在没有显式工作表声明的情况下使用 Cells 属性(它返回活动工作表上的一个单元格)。

注意

虽然可以使用 Visual Basic 字符串函数转换 A1 样式引用,但使用 Cells(1, 1) 表示法更为简便(而且也是更好的编程习惯)。

Sub SetUpTable() Worksheets("Sheet1").Activate For TheYear = 1 To 5 Cells(1, TheYear + 1).Value = 1990 + TheYear Next TheYear For TheQuarter = 1 To 4 Cells(TheQuarter + 1, 1).Value = "Q" & TheQuarter Next TheQuarter End Sub

Use_expression_.Cells,其中 expression 表达式将返回一个 Range 对象,以获取由单个单元格组成的相同地址的区域。 在此区域中,你可以通过 Item(row, column) 访问单个单元格,此位置相对于该区域的第一个区的左上角。 Item 可以省略,因为 Range 的默认成员会将调用转发给它。 以下示例设置活动工作簿第一个工作表单元格 C5 和 D5 的公式。

Worksheets(1).Range("C5:C10").Cells(1, 1).Formula = "=Rand()" Worksheets(1).Range("C5:C10").Cells.Item(1, 2).Formula = "=Rand()"

使用 Range(cell1, cell2) 可返回一个 Range 对象,其中 cell1 和 cell2 是指定起始和终止单元格的 Range 对象。 下例设置单元格 A1:J10 的边框线条样式。

注意

请注意,如果要对 Cells 属性应用前导 With 语句的结果,则每次出现 Cells 属性时其前面必须具有句点。 在本例中,它表示单元格位于工作表 1 上(不带句点,Cells 属性会返回活动工作表上的单元格)。

With Worksheets(1) .Range(.Cells(1, 1), _ .Cells(10, 10)).Borders.LineStyle = xlThick End With

使用工作表上的 Rows 获取由工作表上的所有行组成的区域。 可以通过 Item(row) 访问单个行,其中 row 为行索引。 Item 可以省略,因为 Range 的默认成员会将调用转发给它。

注意

对于包含行的区域,提供 Item 的第二个参数是不合法的。 你首先必须通过 Cells 将其转换为单个单元格。

以下示例删除了活动工作簿第一个工作表的第 5 和第 10 行。

Worksheets(1).Rows(10).Delete Worksheets(1).Rows.Item(5).Delete

使用工作表上的 Columns 获取由工作表上的所有列组成的区域。 可以通过 Item(row) [sic] 访问单个列,其中 row 为列索引,以数值或 A1 样式的列地址表示。 Item 可以省略,因为 Range 的默认成员会将调用转发给它。

注意

对于包含列的区域,提供 Item 的第二个参数是不合法的。 你首先必须通过 Cells 将其转换为单个单元格。

以下示例删除活动工作簿第一个工作表的列“B”、“C”、“E”和“J”。

Worksheets(1).Columns(10).Delete Worksheets(1).Columns.Item(5).Delete Worksheets(1).Columns("C").Delete Worksheets(1).Columns.Item("B").Delete

Use_expression_.Rows,其中 expression 表达式将返回一个 Range 对象,以获取由该区域第一个区中的行组成的区域。 可以通过 Item(row) 访问单个行,其中,row 为该区域第一个区顶部的相对行索引。 Item 可以省略,因为 Range 的默认成员会将调用转发给它。

注意

对于包含行的区域,提供 Item 的第二个参数是不合法的。 你首先必须通过 Cells 将其转换为单个单元格。

以下示例删除活动工作簿第一个工作表的区域 C8:D8 和 C6:D6。

Worksheets(1).Range("C5:D10").Rows(4).Delete Worksheets(1).Range("C5:D10").Rows.Item(2).Delete

Use_expression_.Columns,其中 expression 表达式将返回一个 Range 对象,以获取由该区域第一个区中的列组成的区域。 可以通过 Item(row) [sic] 访问单个列,其中 row 为该区域第一个区左侧的相对列索引,以数值或 A1 样式的列地址表示。 Item 可以省略,因为 Range 的默认成员会将调用转发给它。

注意

对于包含列的区域,提供 Item 的第二个参数是不合法的。 你首先必须通过 Cells 将其转换为单个单元格。

以下示例删除活动工作簿第一个工作表的区域 L2:L10、G2:G10、F2:F10 和 D2:D10。

Worksheets(1).Range("C5:Z10").Columns(10).Delete Worksheets(1).Range("C5:Z10").Columns.Item(5).Delete Worksheets(1).Range("C5:Z10").Columns("D").Delete Worksheets(1).Range("C5:Z10").Columns.Item("B").Delete

使用 Offset(row, column) 可返回相对于另一区域在指定偏移量处的区域,其中 row 和 column 分别是行偏移量和列偏移量。 下例选择从当前选定区域左上角的单元格下移 3 行和右移 1 列所得的单元格。 不能选择活动工作表上没有的单元格,因此必须先激活工作表。

Worksheets("Sheet1").Activate 'Can't select unless the sheet is active Selection.Offset(3, 1).Range("A1").Select

使用 Union (range1, range2, ...) 可返回多区域范围,即返回由两个或更多连续单元格区域构成的范围。 下例创建一个定义为区域 A1:B2 和 C3:D4 的合并区域的对象,然后选择所定义的区域。

Dim r1 As Range, r2 As Range, myMultiAreaRange As Range Worksheets("sheet1").Activate Set r1 = Range("A1:B2") Set r2 = Range("C3:D4") Set myMultiAreaRange = Union(r1, r2) myMultiAreaRange.Select

如果使用包含多个区域的选定范围,则 Areas 属性很有用。 它会将所选定的多区域范围划分为单个 Range 对象,然后以集合的形式返回对象。 可对所返回的集合使用 Count 属性,以验证包含多个区域的选定范围(如下例所示)。

Sub NoMultiAreaSelection() NumberOfSelectedAreas = Selection.Areas.Count If NumberOfSelectedAreas > 1 Then MsgBox "You cannot carry out this command " & _ "on multi-area selections" End If End Sub

此示例使用 Range 对象的 AdvancedFilter 方法在 A 列的区域中创建一个唯一值列表和这些唯一值的出现次数。

Sub Create_Unique_List_Count() 'Excel workbook, the source and target worksheets, and the source and target ranges. Dim wbBook As Workbook Dim wsSource As Worksheet Dim wsTarget As Worksheet Dim rnSource As Range Dim rnTarget As Range Dim rnUnique As Range 'Variant to hold the unique data Dim vaUnique As Variant 'Number of unique values in the data Dim lnCount As Long 'Initialize the Excel objects Set wbBook = ThisWorkbook With wbBook Set wsSource = .Worksheets("Sheet1") Set wsTarget = .Worksheets("Sheet2") End With 'On the source worksheet, set the range to the data stored in column A With wsSource Set rnSource = .Range(.Range("A1"), .Range("A100").End(xlDown)) End With 'On the target worksheet, set the range as column A. Set rnTarget = wsTarget.Range("A1") 'Use AdvancedFilter to copy the data from the source to the target, 'while filtering for duplicate values. rnSource.AdvancedFilter Action:=xlFilterCopy, _ CopyToRange:=rnTarget, _ Unique:=True 'On the target worksheet, set the unique range on Column A, excluding the first cell '(which will contain the "List" header for the column). With wsTarget Set rnUnique = .Range(.Range("A2"), .Range("A100").End(xlUp)) End With 'Assign all the values of the Unique range into the Unique variant. vaUnique = rnUnique.Value 'Count the number of occurrences of every unique value in the source data, 'and list it next to its relevant value. For lnCount = 1 To UBound(vaUnique) rnUnique(lnCount, 1).Offset(0, 1).Value = _ Application.Evaluate("COUNTIF(" & _ rnSource.Address(External:=True) & _ ",""" & rnUnique(lnCount, 1).Text & """)") Next lnCount 'Label the column of occurrences with "Occurrences" With rnTarget.Offset(0, 1) .Value = "Occurrences" .Font.Bold = True End With End Sub 方法 Activate AddComment AddCommentThreaded AdvancedFilter AllocateChanges ApplyNames ApplyOutlineStyles AutoComplete AutoFill AutoFilter AutoFit AutoOutline BorderAround Calculate CalculateRowMajorOrder CheckSpelling Clear ClearComments ClearContents ClearFormats ClearHyperlinks ClearNotes ClearOutline ColumnDifferences Consolidate ConvertToLinkedDataType Copy CopyFromRecordset CopyPicture CreateNames Cut DataTypeToText DataSeries Delete DialogBox Dirty DiscardChanges EditionOptions ExportAsFixedFormat FillDown FillLeft FillRight FillUp 查找 FindNext FindPrevious FlashFill FunctionWizard Group 插入 InsertIndent Justify ListNames Merge NavigateArrow NoteText Parse PasteSpecial PrintOut PrintPreview RemoveDuplicates RemoveSubtotal Replace RowDifferences Run Select SetCellDataTypeFromCell SetPhonetic Show ShowCard ShowDependents ShowErrors ShowPrecedents Sort SortSpecial Speak SpecialCells SubscribeTo Subtotal Table TextToColumns Ungroup UnMerge 属性 AddIndent 地址 AddressLocal AllowEdit 应用程序 Areas Borders Cells Characters 列 列 ColumnWidth Comment CommentThreaded Count CountLarge Creator CurrentArray CurrentRegion Dependents DirectDependents DirectPrecedents DisplayFormat End EntireColumn EntireRow 错误 Font FormatConditions Formula FormulaArray FormulaHidden FormulaLocal FormulaR1C1 FormulaR1C1Local HasArray HasFormula HasRichDataType Height Hidden HorizontalAlignment Hyperlinks ID IndentLevel Interior 项 Left LinkedDataTypeState ListHeaderRows ListObject LocationInTable Locked MDX MergeArea MergeCells 名称 Next NumberFormat NumberFormatLocal Offset Orientation OutlineLevel PageBreak Parent Phonetic Phonetics PivotCell PivotField PivotItem PivotTable Precedents PrefixCharacter Previous QueryTable Range ReadingOrder Resize 行 RowHeight Rows ServerActions ShowDetail ShrinkToFit SoundNote SparklineGroups 样式 摘要 Text Top UseStandardHeight UseStandardWidth 验证 值 Value2 VerticalAlignment Width Worksheet WrapText XPath 另请参阅 Excel 对象模型引用 支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有