Better way to find last used row

前端 未结 8 1149
情歌与酒
情歌与酒 2020-11-22 15:32

I am trying to make this way of finding the last row as I found the last column:

Sheets(\"Sheet2\").Cells(1,Sheets(\"Sheet2\").Columns.Count).End(xlToLeft).C         


        
8条回答
  •  旧时难觅i
    2020-11-22 16:04

    This is the best way I've seen to find the last cell.

    MsgBox ActiveSheet.UsedRage.SpecialCells(xlCellTypeLastCell).Row
    

    One of the disadvantages to using this is that it's not always accurate. If you use it then delete the last few rows and use it again, it does not always update. Saving your workbook before using this seems to force it to update though.

    Using the next bit of code after updating the table (or refreshing the query that feeds the table) forces everything to update before finding the last row. But, it's been reported that it makes excel crash. Either way, calling this before trying to find the last row will ensure the table has finished updating first.

    Application.CalculateUntilAsyncQueriesDone
    

    Another way to get the last row for any given column, if you don't mind the overhead.

    Function GetLastRow(col, row)
        ' col and row are where we will start.
        ' We will find the last row for the given column.
        Do Until ActiveSheet.Cells(row, col) = ""
            row = row + 1
        Loop
        GetLastRow = row
    End Function
    

提交回复
热议问题