2013-03-02 48 views
1

如何从Activesheet删除空白单元格后重新计算Activesheet的最后一行,以便我得到一个新的lastRow变量是我的最大限制?我的数据来自网络查询。在ActiveSheet中删除空白单元格后,在VBA中重新计算lastRow

实施例:

  1. 数据源:1360
  2. 数量的行删除空白细胞后:http://en.wikipedia.org/wiki/Polar_bear
  3. 导入后的行数650

ActiveSheet不刷新和lastRow没有更新到650,我希望那些更新

+5

在你的代码中使用'ActiveSheet.UsedRange'将重新计算使用的范围 – brettdj 2013-03-02 07:09:17

回答

1

由于这是编写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()函数已删除的行之后。

+0

+1很好的便携式方法 – whytheq 2013-03-03 17:51:42

相关问题