所有提到的那些自然分開,還有其他一些方法可以找到在最後的行或列工作表或指定範圍。
Function FindingLastRow(col As String) As Long
'PURPOSE: Various ways to find the last row in a column or a range
'ASSUMPTION: col is passed as column header name in string data type i.e. "B", "AZ" etc.
Dim wks As Worksheet
Dim lstRow As Long
Set wks = ThisWorkbook.Worksheets("Sheet1") 'Please change the sheet name
'Set wks = ActiveSheet 'or for your problem uncomment this line
'Method #1: By Finding Last used cell in the worksheet
lstRow = wks.Range("A1").SpecialCells(xlCellTypeLastCell).Row
'Method #2: Using Table Range
lstRow = wks.ListObjects("Table1").Range.Rows.Count
'Method #3 : Manual way of selecting last Row : Ctrl + Shift + End
lstRow = wks.Cells(wks.Rows.Count, col).End(xlUp).Row
'Method #4: By using UsedRange
wks.UsedRange 'Refresh UsedRange
lstRow = wks.UsedRange.Rows(wks.UsedRange.Rows.Count).Row
'Method #5: Using Named Range
lstRow = wks.Range("MyNamedRange").Rows.Count
'Method #6: Ctrl + Shift + Down (Range should be the first cell in data set)
lstRow = wks.Range("A1").CurrentRegion.Rows.Count
'Method #7: Using Range.Find method
lstRow = wks.Column(col).Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
FindingLastRow = lstRow
End Function
注意:因爲它證明您的問題聲明請只用上面的方法之一。
請注意,查找方法沒有看到單元格格式,但只有數據,因此只查找xlCellTypeLastCell(如果數據很重要而不格式化)。另外,合併的單元格(必須避免)可能會給您帶來意想不到的結果,因爲它會爲您提供第一個單元格的行號,而不是合併單元格中的最後一個單元格。
的重複來代替[在行上次不是空的細胞; Excel的VBA(http://stackoverflow.com/questions/4872512/last-not-empty-cell-in-row-excel-vba) – brettdj
請參見我的文章在https://stackoverflow.com/questions/71180/how -can-i-find-last-row-that-contains-data-in-the-excel-sheet-with-a-macro/46419169#46419169我認爲它可以解決你的請求。 –
奇怪的是,我運行下面的代碼,並將其與空行之間的工作,即使在:ActiveSheet.Cells(ActiveSheet.Rows.count,3).END(xlUp).Row' '_(EXCEL 2016在MacOS X )_ – KJH