范围中的最后一行 - 最后一行不是整个工作表上的最后一行

时间:2013-10-31 20:15:09

标签: excel-vba range vba excel

我四处寻找如何找到范围中的最后一行,但我找到的答案 - 充其量 - 只给我整张表中最后一行。

例如,假设我在A1:A10B6:B9C1:C4中有元素。我想在列B:C中找到最后一行。在这种情况下,答案应该是第9行。我已经尝试使用SpecialCells(xlLastCell),但只得到第10行的答案。

我确信这是一个非常简单的答案,但我找不到它!有人可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

以下是如何使用VBA返回列B:C中最后一行的编号的示例:

Sub ReturnLastRow()
    MsgBox "Last row in columns B:C is " & _
        WorksheetFunction.Max(ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row, ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row)
End Sub

答案 1 :(得分:1)

对于任何范围 r

nLastRow = r.Rows.Count + r.Row - 1
MsgBox ("last row " & nLastRow)

nLastColumn = r.Columns.Count + r.Column - 1
MsgBox ("last column " & nLastColumn)

nFirstRow = r.Row
MsgBox ("first row " & nFirstRow)

nFirstColumn = r.Column
MsgBox ("first column " & nFirstColumn)

numrow = r.Rows.Count
MsgBox ("number of rows " & numrow)

numcol = r.Columns.Count
MsgBox ("number of columns " & numcol)

答案 2 :(得分:0)

要获取特定范围中的最后一行,不一定是特定行,您可以简单地引用该范围中的最后一个单元格,类似于使用数组的方式:

Set RR = Range("B6:B9", "C4:C11")
MsgBox "The Last Row is " & RR(RR.Count).Row

OR

Set RR = Range("B6:B11", "C4:C8")
MsgBox "The Last Row is " & RR(RR.Count).Row

两者都将返回11作为上例中的最后一行。

相关问题