由于这是编写VBA for Excel的常见要求,因此您可能希望编写一个可在其他项目中重用的函数。这里有一个例子:
Function get_end_row(ByVal column_with_data As Long) As Long
Dim last_row As Long
last_row = ThisWorkbook.ActiveSheet.Rows.Count
get_end_row = ThisWorkbook.ActiveSheet.Cells(last_row, column_with_data).End(xlUp).Row
''Note: in the line of code above you can replace xlUp with its hexidecimal value -> &HFFFFEFBE
''This will ensure the function works in other versions of Excel
End Function
这里是函数是如何被调用的例子:
Sub my_routine()
Dim endRow As Long
''The 1 being passed in the argument below is the first column number that your data is in
endRow = get_end_row(1)
Msgbox endRow
End Sub
在你的情况下,确保你调用get_end_row()函数已删除的行之后。
在你的代码中使用'ActiveSheet.UsedRange'将重新计算使用的范围 – brettdj 2013-03-02 07:09:17