VBA LastRow计算不起作用

我有一个自动筛选的工作表,从单元格B3开始。 列A包含一些macrosbutton,但实际上是空白的。 前两行包含有关主范围内数据的信息。

在VBA中,我使用我认为是确定工作表中最后一行的标准方法(在这种情况下,我不能依赖单列上的.End方法):

 LastRow = Activesheet.Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row 

但是,有时这会返回一个值,即使我有数千行数据。 它似乎只有当有filter设置(但仍然有数据可见行),但即使这样并不总是发生,我看不到一个模式。

我知道还有其他的解决办法 – 我已经改为UsedRange技术,但是这个特殊的失败是非常令人沮丧的,因为在这种情况下它是最有效的。

有谁知道为什么会这样呢?

你有没有想过使用格雷格的答案,但循环find所有列的最高行? 就像是:

 LastRow = 1 With ActiveSheet For i = 1 to .UsedRange.Columns.Count If .Cells(.Rows.Count, i).End(xlUp).Row > LastRow Then LastRow = .Cells(.Rows.Count, i).End(xlUp).Row EndIf Next i End With 

这个解决scheme将允许在底部行中随机填充空白值。 UsedRange是非常棘手的,因为它会返回已编辑过的最远的行/列(即使它现在是空白的)。 根据我的经验,如果在工作表中按下Ctrl-Up,则Range.End(xlUp)的行为与您所期望的相同。 这是更可预测的一点。

如果你使用.Find,试试看After:= [A1]参数。 我还没有探讨这个function的特质,但那将是我开始给这个问题的地方。

尝试这个…

 Dim LastRow as long With ActiveSheet LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With 

这将得到A列的最后一行

build议您尝试使用Find指定将XlFormulas视为与XlValues不同,隐藏的单元格(但未过滤的单元格)将使用此参数进行检测。

 Sub Test() Dim rng1 As Range Set rng1 = ActiveSheet.Cells.Find("*", [a1], xlFormulas, , xlByRows, xlPrevious) If Not rng1 Is Nothing Then MsgBox rng1.Row End Sub 

今天早上我面对同样的问题。

起初,我确信“.find”function是不稳定的。

但是,在摆弄了一段时间之后,我发现我的表格中行号太深的非空单元格,认为它是1,000或10,000或类似的。 我删除它,“.find”再次运作。 可能某些内部VBAvariables的限制不够大。

做这个:

1)按CTRL + END

2)识别非空单元格,假设这是无意中填入的,并删除它。

尝试下面的代码:

 Sub GetColA_LastRow() Dim ws As Worksheet Dim lRow As Long Set ws = ThisWorkbook.Sheets("Sheet1") With ws lRow = .Range("A" & .Rows.Count).End(xlUp).Row End With MsgBox "The last row which has data in Col A of Sheet1 is " & lRow End Sub 

要么

 sub getLastRow() dim lastRow as long lastRow = Sheets("sheet1").Range("A65000").End(xlUp).Row end sub 

您也可以访问链接了解更多详情http://www.siddharthrout.com/2012/10/02/find-last-row-in-an-excel-sheetvbavb-net/

更新评论后的代码:

 Sub getLastRow() Dim rng As Range, lastRow As Long Set rng = Cells.Find("mango") ' here you enter whatever you want to find If Not rng Is Nothing Then lastRow = Sheets("sheet1").Cells(65000, rng.Column).End(xlUp).Row End If End Sub 

怎么样:

 with activesheet.usedrange LastRow = .rows.count end with 

菲利普

我的类似的问题是什么是最后一行和col使用,不pipe有没有过滤之前的空单元格。 我从我能find的零碎中拼凑出来,至less对于填充数据的单元格来说,这是我和你所想的。

 Function FindLastUsedRowAndCol(ByVal ws As Worksheet) As Variant() Dim LastColRange As Range Dim LastCol As Integer Dim LastRow As Long Dim LastRowTmp As Long Dim RowXCol(2) As Variant Set LastColRange = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _ xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False) LastCol = LastColRange.Column LastRow = 1 For i = 1 To LastCol Step 1 If ws.FilterMode Then LastRow = ws.AutoFilter.Range.Rows.Count LastRowTmp = Cells(ws.Rows.Count, i).End(xlUp).row If LastRowTmp > LastRow Then LastRow = LastRowTmp Else LastRowTmp = Cells(ws.Rows.Count, i).End(xlUp).row If LastRowTmp > LastRow Then LastRow = LastRowTmp End If Next i RowXCol(1) = LastRow RowXCol(2) = LastCol FindLastUsedRowAndCol = RowXCol End Function 

并testing:

 Sub testit() Dim ws As Worksheet Set ws = Application.Worksheets("Sheet1") cr = FindLastUsedRowAndCol(ws) MsgBox "row: " & cr(1) & " col: " & cr(2) End Sub