2013-10-31 161 views
0

我查找過如何查找範圍中的最後一行,但我發現的答案可能 - 最好 - 只給出我是整個工作表的最後一行。範圍內的最後一行 - 最後一行不是整個表格中的最後一行[解決]

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

我確定有一個非常簡單的答案,但我找不到它!誰能幫忙?

回答

1

下面是如何在B:C列與VBA返回最後一行的數字爲例:

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 
+0

呵呵,爽!我很確定這正是我要找的。我很高興明天測試它! – user2815713

+0

@SørenHoltenHansen+1好答案 – Santosh

+0

@SørenHoltenHansen,謝謝!只需要嘗試一下。奇蹟般有效。 – user2815713

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作爲上例中的最後一行。

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)