在VBA中删除空白单元格之后,在ActiveSheet中重新计算lastRow

如何从Activesheet删除空白单元格后,如何重新计算Activesheet的lastRow,这样我会得到一个新的lastRowvariables是我的最大限制? 我的数据来自networking查询。

例:

  1. 数据来源: http : //en.wikipedia.org/wiki/Polar_bear
  2. 导入后的行数:1360
  3. 删除空白单元格后的行数:650

ActiveSheet不刷新和lastRow不更新到650,我想这些更新

由于这是编写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()函数。