使用VBA代码确定激活选择格区域

上传人:ji****72 文档编号:40493094 上传时间:2018-05-26 格式:DOC 页数:20 大小:106KB
返回 下载 相关 举报
使用VBA代码确定激活选择格区域_第1页
第1页 / 共20页
使用VBA代码确定激活选择格区域_第2页
第2页 / 共20页
使用VBA代码确定激活选择格区域_第3页
第3页 / 共20页
使用VBA代码确定激活选择格区域_第4页
第4页 / 共20页
使用VBA代码确定激活选择格区域_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《使用VBA代码确定激活选择格区域》由会员分享,可在线阅读,更多相关《使用VBA代码确定激活选择格区域(20页珍藏版)》请在金锄头文库上搜索。

1、单元格区域引用方式的小结单元格区域引用方式的小结单元格区域引用方式的小结单元格区域引用方式的小结 EXCEL 技巧 2008-11-29 12:10:20 阅读 97 评论 0 字号:大中小 订阅 在使用 ExcelVBA 进行编程时,我们通常需要频繁地引用单元格区域,然后再使用相应的属性和方法对区域进行操作。所谓单元格区域,指的是单个的单元格、或者是由多个单元格组成的区域、或者是整行、整列等。下面,我们设定一些情形,以问答的形式对引用单元格区域的方式进行归纳。 -问题一:在 VBA 代码中,如何引用当前工作表中的单个单元格(例如引用单元格 C3)?回答:可以使用下面列举的任一方式对当前工作表

2、中的单元格(C3)进行引用。(1) Range(“C3“)(2) C3(3) Cells(3, 3)(4) Cells(3, “C“)(5) Range(“C4“).Offset(-1)Range(“D3“).Offset(, -1)Range(“A1“).Offset(2, 2)(6) 若 C3 为当前单元格,则可使用:ActiveCell(7) 若将 C3 单元格命名为“Range1”,则可使用:Range(“Range1“)或Range1(8) Cells(4, 3).Offset(-1)-问题二:在 VBA 代码中,我要引用当前工作表中的 B2:D6 单元格区域,有哪些方式?回答:可以

3、使用下面列举的任一方式对当前工作表中单元格区域 B2:D6 进行引用。(1) Range(“B2:D6”)(2) Range(“B2“, “D6“)(3) B2:D6(4) Range(Range(“B2“), Range(“D6“)(5) Range(Cells(2, 2), Cells(6, 4)(6) 若将 B2:D6 区域命名为“MyRange”,则又可以使用下面的语句引用该区域: Range(“MyRange“) MyRange(7) Range(“B2“).Resize(5, 3)(8) Range(“A1:C5“).Offset(1, 1)(9) 若单元格 B2 为当前单元格,则

4、可使用语句:Range(ActiveCell, ActiveCell.Offset(4, 2)(10) 若单元格 D6 为当前单元格,则可使用语句:Range(“B2“, ActiveCell)-问题三:在 VBA 代码中,如何使用变量实现对当前工作表中不确定单元格区域的引用?回答:有时,我们需要在代码中依次获取工作表中特定区域内的单元格,这通常可以采取下面的几种方式:(1) Range(“A” 2) xlNumbers(包含数字);3) xlErrors(包含错误值); 4) xlLogical(包含逻辑值)自已在工作表输入一些含有数值和公式的数据,隐藏或不隐藏最后一行或公式所在的行,先体验

5、下面的两段示例代码。示例代码 08当最后一行为公式或隐藏了最后行时,会忽略,即认为倒数第二行为最后一行Sub NextConstantRowFunction()Range(“A“ & LastConstantRow(True, True, True, True) + 1).SelectEnd Sub- - - - - - - - - - - - - - - - - - - - - - - - Public Function LastConstantRow(Optional IncludeText As Boolean, _Optional IncludeNumbers As Boolean,

6、_Optional IncludeErrors As Boolean, _Optional IncludeLogicals As Boolean) As LongDim Text As Long, Numbers As Long, Errors As LongDim Logical As Long, AllTypes As LongIf IncludeText Then Text = xlTextValues Else Text = 0If IncludeNumbers Then Numbers = xlNumbers Else Numbers = 0If IncludeErrors Then

7、 Errors = xlErrors Else Errors = 0If IncludeLogicals Then Logical = xlLogical Else Logical = 0AllTypes = Text + Numbers + Errors + LogicalOn Error GoTo FinishLastConstantRow = Split(Cells.SpecialCells(xlCellTypeConstants, AllTypes).Address, “$“) _(UBound(Split(Cells.SpecialCells(xlCellTypeConstants,

8、 AllTypes).Address, “$“)Exit FunctionFinish:MsgBox “没有发现数据!“End Function示例代码 09查找含有公式的单元格所在的行,忽略该行以后的常量和隐藏的行Sub NextFormulaRowFunction()Range(“A“ & LastFormulaRow(True, True, True, True) + 1).SelectEnd Sub- - - - - - - - - - - - - - - - - - - Public Function LastFormulaRow(Optional IncludeText As Bo

9、olean, _Optional IncludeNumbers As Boolean, _Optional IncludeErrors As Boolean, _Optional IncludeLogicals As Boolean) As LongDim Text As Long, Numbers As Long, Errors As LongDim Logical As Long, AllTypes As LongIf IncludeText Then Text = xlTextValues Else Text = 0If IncludeNumbers Then Numbers = xlN

10、umbers Else Numbers = 0If IncludeErrors Then Errors = xlErrors Else Errors = 0If IncludeLogicals Then Logical = xlLogical Else Logical = 0AllTypes = Text + Numbers + Errors + LogicalOn Error GoTo FinishLastFormulaRow = Split(Cells.SpecialCells(xlCellTypeFormulas, AllTypes).Address, “$“) _(UBound(Spl

11、it(Cells.SpecialCells(xlCellTypeFormulas, AllTypes).Address, “$“)Exit FunctionFinish:MsgBox “没有发现数据!“End Function下面的示例代码 10 忽略最后一行带有公式的单元格,即当最后一行的单元格中含有公式时,将倒数第二行作为最后一行,即只考虑直接输入到工作表中的数据。当最后一行没有公式但被隐藏时,并不影响该方法的判断。示例代码 10Sub SpecialCells_LastRowxlCellTypeConstants()Dim MyRow As RangeOn Error GoTo Fini

12、shSet MyRow = Intersect(A:A, Cells. _SpecialCells(xlCellTypeConstants).EntireRow).EntireRow获取最后一行MsgBox “最后一行是第“ & Split(MyRow.Address, “$“) _(UBound(Split(MyRow.Address, “$“) & “行”Set MyRow = NothingExit SubFinish:MsgBox “没有发现数据!“End Sub注:因为上述代码使用了Split函数,故只适合于 Office2000 及以上的版本。该方法也允许我们指定单个数据类型,诸如

13、数字数据或文本数据,如下所示。下面,我们查找的最后一行是仅在行中有数字(而不包含公式)的单元格的最后一行。示例代码 11Sub SpecialCells_LastRowxlCellTypeNumberConstants()Dim MyRow As RangeOn Error GoTo FinishSet MyRow = Intersect(A:A, Cells. _SpecialCells(xlCellTypeConstants, xlNumbers).EntireRow)获取最后一行MsgBox “最后一行是第“ & Split(MyRow.Address, “$“) _(UBound(Sp

14、lit(MyRow.Address, “$“) & “行”Set MyRow = NothingExit SubFinish:MsgBox “没有发现数据!“End Sub下面,我们查找的最后一行是仅在行中有文本(而不包含公式)的单元格的最后一行。示例代码 12Sub SpecialCells_LastRowxlCellTypeTextConstants()Dim MyRow As RangeOn Error GoTo FinishSet MyRow = Intersect(A:A, Cells. _SpecialCells(xlCellTypeConstants, xlTextValues)

15、.EntireRow)获取最后一行MsgBox “最后一行是第“ & Split(MyRow.Address, “$“) _(UBound(Split(MyRow.Address, “$“) & “行”Set MyRow = NothingExit SubFinish:MsgBox “没有发现数据!“End Sub下面,我们查找的最后一行是仅在行中有公式的单元格的最后一行。示例代码 13Sub SpecialCells_LastRowxlCellTypeFormulas()Dim MyRow As RangeOn Error GoTo FinishSet MyRow = Intersect(A:A, Cells. _SpecialCells(xlCellTypeFormulas).EntireRow).EntireRow获取最后一行MsgBox “最后一行是第“ & Split(MyRow.Address, “$“) _(UBound(Split(MyRow.Address, “$“) & “行”Set MyRow = NothingExit SubFinish:

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号