Better way to find last used row

前端 未结 8 1088
情歌与酒
情歌与酒 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条回答
  •  失恋的感觉
    2020-11-22 16:19

    I use the following function extensively. As pointed out above, using other methods can sometimes give inaccurate results due to used range updates, gaps in the data, or different columns having different row counts.

    Example of use:

    lastRow=FindRange("Sheet1","A1:A1000")
    

    would return the last occupied row number of the entire range. You can specify any range you want from single columns to random rows, eg FindRange("Sheet1","A100:A150")

    Public Function FindRange(inSheet As String, inRange As String) As Long
        Set fr = ThisWorkbook.Sheets(inSheet).Range(inRange).find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        If Not fr Is Nothing Then FindRange = fr.row Else FindRange = 0
    End Function
    

提交回复
热议问题