簡體   English   中英

范圍中的最后一行 - 最后一行不是整個工作表上的最后一行

[英]Last Row in a Range - last row is NOT last used row on entire sheet

我四處尋找如何找到范圍內的最后一行,但我找到的答案 - 充其量 - 只給我整張表的最后一行。

例如,假設我在A1:A10有元素A1:A10B6:B9C1:C4 我想找到B:C列中的最后一行B:C 在這種情況下,答案應該是第9行。我已經嘗試過使用SpecialCells(xlLastCell) ,但只獲得了第10行的答案。

我確信這是一個非常簡單的答案,但我找不到它! 有人可以幫忙嗎?

以下是如何使用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

對於任何范圍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)

要獲取特定范圍中的最后一行,不一定是特定行,您可以簡單地引用該范圍中的最后一個單元格,類似於使用數組的方式:

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

要么

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

兩者都將返回11作為上例中的最后一行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM