找到最后使用的行的更好方法 [英] Better way to find last used row

查看:26
本文介绍了找到最后使用的行的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以与找到最后一列相同的方式找到最后一行:

I am trying to find the last row the same way I found the last column:

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

我知道这种方式,但它不如之前的有用:

I know this way but it is not as helpful as the prior would be:

u = Sheets("Sheet1").Range("A65536").End(xlUp).Row

我试过了:

Sheets("Sheet2").Cells(Sheets("Sheet2",1).Rowa.Count).End(xlToUP).Column

概要:我想要最后一行的以下方式.

Synopsis: I would like the below way for last row.

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

推荐答案

您应该使用 with 语句来限定您的 RowsColumns 计数.这将防止在使用 2007 年之前和较新的 2007 Excel 工作簿时出现任何错误.

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.

最后一列

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

最后一行

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

或者

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

这篇关于找到最后使用的行的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆