excel - 找到最后使用的行的更好方法

标签 excel vba

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

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

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

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

我尝试过:

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

概要:我希望最后一行采用以下方式。

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

最佳答案

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

最后一栏

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 

关于excel - 找到最后使用的行的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38882321/

相关文章:

vba - 键入时格式化 TextBox 中的数字

xml - 我可以使用 VBA 中的变量更新 XML 节点值吗

excel - 如何循环遍历一个表列来过滤另一个表并通过电子邮件发送每个过滤后的表?

excel - 无法将 Excel 数据透视表行标签排序为数字

excel - 如果分隔符是;,如何使用 GNU Octave 读取 CSV 文件?

excel - 更改工作簿名称的一部分并将文件另存为新名称

vba - 如何从 VBA 创建单独的 CSV 文件?

vba - Excel VBA代码选择所有数据有时有效的单元格

excel - 在 Excel 中将矩阵表转换为单行(无 VBA)

excel - WorksheetFunction.Filter 中的数据类型错误