Beh a parte tutte quelle citate, ci sono molti altri modi per trovare l'ultima riga o colonna in un foglio di lavoro o intervallo specificato.
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
Nota: Si prega di utilizzare solo uno dei metodi di cui sopra come giustifica la sua dichiarazione problema.
Si prega di prestare attenzione al fatto che il metodo di ricerca non visualizza la cella ma solo i dati, quindi cercare xlCellTypeLastCell se solo i dati sono importanti e non la formattazione. Inoltre, le celle unite (che devono essere evitate) potrebbero fornire risultati imprevisti in quanto forniscono il numero di riga della prima cella e non l'ultima cella nelle celle unite.
duplicato di [ultima cella non vuota in fila; Excel VBA] (http://stackoverflow.com/questions/4872512/last-not-empty-cell-in-row-excel-vba) – brettdj
Vedere il mio post su https://stackoverflow.com/questions/71180/how -can-i-find-last-row-che-contiene-data-in-the-excel-sheet-with-a-macro/46419169 # 46419169 Penso che possa risolvere la tua richiesta. –
Stranamente, sto usando il seguente codice e funziona anche con le righe vuote tra: 'ActiveSheet.Cells (ActiveSheet.Rows.count, 3) .End (xlUp) .Row' _ (Excel 2016 su MacOS X) _ – KJH