1、A & Rows.Count).End(xlUp) = Empty Then GoTo Finish 获取最后一行 MsgBox 最后一行是第 Range( Rows.Count).End(xlUp).Row & “行.” Exit Sub Finish:没有发现公式或数据! End Sub示例代码02 Sub NextRowInColumnUsedAsSub() 包含所有数据和公式,忽略隐藏的最后一行 Range( Rows.Count).End(xlUp).Row + 1).Select End Sub示例代码03 Sub NextRowInColumnUsedAsFunction() L
2、astRowInColumn() + 1).Select End Sub - - - - - - - - - - - - - - - - - - - - - - - - - Public Function LastRowInColumn(Column As String) As Long LastRowInColumn = Range(Column & Rows.Count).End(xlUp).Row End Function 注意,要输入新数据的列可能与我们所查找最后一行时所使用的列不同,例如,在上例中,我们可以修改为在B列中查找该列的最后一行,而在A列相应行的下一行中输入新的数据。 -
3、使用Find方法 Find方法在当前工作有数据中进行查找,不需要指定列,也可以确保不会意外地覆盖掉已有数据。其中,参数LookIn指定所查找的类型,有三个常量可供选择,即xlValues、xlFormulas和xlComments。(1) 常量xlFormulas将包含零值的单元格作为有数据的单元格。(当设置零值不显示时,该单元格看起来为空,但该参数仍将该单元格视为有数据的单元格) (2) 常量xlValues将包含零值的单元格(如果设置零值不显示时)作为空白单元格,此时,若该单元格在最后一行,则Find方法会认为该单元格所在的行为空行,因此,该单元格中的内容可能会被新数据所覆盖。 注:在Ex
4、cel中,选择菜单“工具”“选项”,在打开的“选项”对话框中,选择“视图”选项卡,将其中的“零值”前的复选框取消选中,则工作表中的零值都不会显示 如果在参数LookIn中使用常量xlValues的话,还存在一个问题是:如果您将最后一行隐藏,则Find方法会认为倒数第二行是最后一行,此时您在最后一行的下一行输入数据,则会将实际的最后一行的数据覆盖。 您可以在隐藏最后一行与不隐藏最后一行,或者是最后一行显示零值与不显示零值时,运行下面的示例代码04,看看所得的结果有什么不同。示例代码04 Sub Find_LastRowxlValues() On Error GoTo Finish MsgBox
5、Cells.Find(*, _ SearchOrder:=xlByRows, LookIn:=xlValues, _ SearchDirection:=xlPrevious).EntireRow.Row & “行” Exit Sub Finish:没有发现数值!因此,在使用Find方法时,您应该考虑所选参数设置的常量,以及工作表最后一行是否有可能被隐藏或不显示零值。如果您忽视这些情况,很可能得不到您想要的结果,或者是覆盖掉已有数据。使用常量xlFormulas可以避免这个问题,如下面的示例代码05所示。示例代码05 Sub Find_LastRowxlFormulas() =xlFormula
6、s, LastRow = Cells.Find(, LookIn:=xlFormulas, SearchDirection:=xlPrevious).Row 注:Find方法中,参数LookIn的默认值为xlFormulas。使用SpecialCells方法 SpecialCells方法用于查找指定类型的值,其语法为SpecialCells(Type,Value),有两种主要的使用方式:(1) 若参数Type仅考虑常量,则在查找时会忽略和覆盖由公式生成的任何数据,如示例代码08所示。(2) 若参数Type仅考虑由公式生成的数据,则在查找时会忽略和覆盖任何常量数据,如示例代码09所示。 如果参数
7、Type是xlCellTypeConstants或者是xlCellTypeFormulas,则Value参数可使用常量决定哪种类型的单元格将被包含在结果中,这些常量值能组合而返回多个类型,其缺省设置是选择所有的常量或公式,而不管是何类型,可使用下面四个可选的常量: 1) xlTextValues(包含文本); 2) xlNumbers(包含数字);3) xlErrors(包含错误值); 4) xlLogical(包含逻辑值) 自已在工作表输入一些含有数值和公式的数据,隐藏或不隐藏最后一行或公式所在的行,先体验下面的两段示例代码。示例代码08 当最后一行为公式或隐藏了最后行时,会忽略,即认为倒数
8、第二行为最后一行 Sub NextConstantRowFunction() LastConstantRow(True, True, True, True) + 1).Select End Sub - - - - - - - - - - - - - - - - - - - - - - - - Public Function LastConstantRow(Optional IncludeText As Boolean, _ Optional IncludeNumbers As Boolean, _ Optional IncludeErrors As Boolean, _ Optional In
9、cludeLogicals As Boolean) As Long Dim Text As Long, Numbers As Long, Errors As Long Dim Logical As Long, AllTypes As Long If IncludeText Then Text = xlTextValues Else Text = 0 If IncludeNumbers Then Numbers = xlNumbers Else Numbers = 0 If IncludeErrors Then Errors = xlErrors Else Errors = 0 If Inclu
10、deLogicals Then Logical = xlLogical Else Logical = 0 AllTypes = Text + Numbers + Errors + Logical On Error GoTo Finish LastConstantRow = Split(Cells.SpecialCells(xlCellTypeConstants, AllTypes).Address, $) _ (UBound(Split(Cells.SpecialCells(xlCellTypeConstants, AllTypes).Address, ) Exit Function Fini
11、sh:没有发现数据!示例代码09 查找含有公式的单元格所在的行,忽略该行以后的常量和隐藏的行 Sub NextFormulaRowFunction() LastFormulaRow(True, True, True, True) + 1).Select- - - - - - - - - - - - - - - - - - - Public Function LastFormulaRow(Optional IncludeText As Boolean, _ Optional IncludeNumbers As Boolean, _ Optional IncludeErrors As Boolea
12、n, _Dim Text As Long, Numbers As Long, Errors As Long Dim Logical As Long, AllTypes As Long If IncludeNumbers Then Numbers = xlNumbers Else Numbers = 0 LastFormulaRow = Split(Cells.SpecialCells(xlCellTypeFormulas,AllTypes).Address, (UBound(Split(Cells.SpecialCells(xlCellTypeFormulas,AllTypes).Address, Exit Function End Fu
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1