Better way to find last used row

前端 未结 8 1150
情歌与酒
情歌与酒 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 15:57

    You should use a with statement to qualify both your Rows and Columns counts. This will prevent any errors while working with older pre 2007 and newer 2007 Excel Workbooks.

    Last Column

    With Sheets("Sheet2")
        .Cells(1, .Columns.Count).End(xlToLeft).Column
    End With 
    

    Last Row

    With Sheets("Sheet2")
        .Range("A" & .Rows.Count).End(xlUp).Row
    End With 
    

    Or

    With Sheets("Sheet2")
        .Cells(.Rows.Count, 1).End(xlUp).Row
    End With 
    

提交回复
热议问题