简体   繁体   English

VBA LastRow计算无效

[英]VBA LastRow calculation not working

I have a worksheet with an autofiltered range that starts in cell B3 . 我有一个自动过滤范围的工作表,从单元格B3开始。 Column A contains some macro buttons but is effectively blank. A列包含一些宏按钮,但实际上是空白的。 The top two rows contain information about the data in the main range. 前两行包含有关主范围内数据的信息。

In VBA, I am using what I believe is a standard method for determining the last row in a worksheet (In this case I cannot rely on the .End method on a single column): 在VBA中,我使用的是我认为是确定工作表中最后一行的标准方法(在这种情况下,我不能依赖单个列上的.End方法):

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

However, sometimes this returns a value of one, even when I have thousands of rows of data. 但是,有时这会返回值1,即使我有数千行数据。 It seems to only do this when there are filters set (but there are still visible rows with data in them), but even then it doesn't always happen and I can't see a pattern to it. 它似乎只在设置过滤器时才会这样做(但是仍然有可见的行包含数据),但即使这样,它也不会总是发生,我看不到它的模式。

I know there are other solutions - I have changed to a UsedRange technique instead, but it is very frustrating that this particular one fails as it would otherwise be the most effective one in this situation. 我知道还有其他的解决方案 - 我已经改为使用了UsedRange技术,但是这个特殊的技术失败是非常令人沮丧的,因为在这种情况下它会是最有效的。

Does anyone know why this would be happening? 有谁知道为什么会发生这种情况?

try this... 试试这个...

Dim LastRow as long

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

This will get the last row in column A. 这将获得A列的最后一行。

Have you thought of using Greg's answer, but looped to find the highest row of all the columns? 您是否考虑过使用Greg的答案,但是要找到所有列的最高行? Something like: 就像是:

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

This solution would allow for blank values randomly populated in bottom rows. 此解决方案将允许在底行中随机填充空白值。 UsedRange is tricky as it will return the furthest outlying row/column that has ever been edited (even if it is currently blank). UsedRange很棘手,因为它将返回已编辑过的最远的行/列(即使它当前是空白的)。 In my experience Range.End(xlUp) behaves as you would expect if you pressed Ctrl-Up while in a worksheet. 根据我的经验,Range.End(xlUp)的行为与您在工作表中按Ctrl-Up时的预期相同。 This is a little more predictable. 这有点可预测。

If you are set on using .Find try looking into the After:=[A1] argument. 如果你设置使用.Find尝试查看After:= [A1]参数。 I haven't explored the idiosyncrasies of this function, but that would be the place I'd start given this problem. 我没有探究过这个函数的特性,但那是我开始给出这个问题的地方。

Suggest that you try using Find specifying to lookin XlFormulas as unlike XlValues , hidden cells (but not filtered cells) will be detected with this argument. 建议您尝试使用Find指定来查找XlFormulasXlValues不同,将使用此参数检测隐藏的单元格(但不是已过滤的单元格)。

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

I faced the exact same issue this morning. 今天早上我遇到了同样的问题。

At first, I was convinced that ".find" feature is unstable. 起初,我确信“.find”功能不稳定。

But, after fiddling for a while, I found out a non-empty cell at a row number way too deep in my Sheet, think it was 1,000 or 10,000 or similar. 但是,在摆弄了一段时间之后,我发现在我的工作表中行数太深的非空单元格,认为它是1,000或10,000或类似。 I deleted it and ".find" works again. 我删除了它,“。find”再次起作用。 Probably, the limits of certain internal VBA variables are not big enough. 可能某些内部VBA变量的限制不够大。

Do this: 做这个:

1) Press CTRL + END 1)按CTRL + END

2) Identify the non-empty cell(s), assuming this is inadvertently filled in, and delete it. 2)识别非空单元格,假设这是无意中填写的,并删除它。

Try below code : 试试以下代码:

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

OR 要么

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

end sub

you may also visit the link for more details http://www.siddharthrout.com/2012/10/02/find-last-row-in-an-excel-sheetvbavb-net/ 您也可以访问该链接了解更多详情http://www.siddharthrout.com/2012/10/02/find-last-row-in-an-excel-sheetvbavb-net/

Update the code after comments : 评论后更新代码:

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

how about: 怎么样:

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

hth Philip 菲利普

My similar question was what's the last row and col used regardless of empty cells before with or without filtering. 我的类似问题是在使用或不使用过滤之前,无论空单元格如何都使用了最后一行和col。 I cobbled this together from bits and pieces I could find and it does what I think both you and I want, at least for cells populated with data. 我从可以找到的点点滴滴拼凑起来,它做了我认为你和我想要的,至少对于填充数据的单元。

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

And to test: 并测试:

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

I know this is an old post but i have seen exactly this problem and not seen any answers that deal with the issue. 我知道这是一个老帖子,但我已经看到了这个问题,没有看到任何解决问题的答案。 It seems to occur sometimes on a dataset where there are rows hidden immediately after the last row. 似乎有时会在数据集上出现,在最后一行之后会立即隐藏行。 It doesn't matter whether you set to lookin xlformulas or xlvalues and i have tried every permutation of the find command i can find and it persistently returns the value 1. (As OP says) The solutions above don't fix this. 无论你是设置查找xlformulas还是xlvalues并且我已经尝试了我能找到的find命令的每个排列并且它持续返回值1.(如OP所说)上面的解决方案不能解决这个问题。 I had to create a function which iterates to find the last row in this case (key bit of code below - in my case i needed to find lastrow in first two columns of various datasheets): 我必须创建一个函数,在这种情况下迭代查找最后一行(下面的代码的关键位 - 在我的情况下,我需要在各种数据表的前两列中找到lastrow):

On Error GoTo ExitLoop
StartRow = 1
LastRow = .Columns("A:B").Find(What:="*", SearchDirection:=xlNormal, LookIn:=xlValues, SearchOrder:=xlByRows).Row
StartRow = LastRow + 1
Do Until WorksheetFunction.CountA(.Range(.Cells(StartRow, 1), .Cells(1048576, 2))) = 0
    FindLastRow = .Range(.Cells(StartRow, 1), .Cells(1048576, 2)).Find(What:="*", SearchDirection:=xlNormal, LookIn:=xlValues, SearchOrder:=xlByRows).Row
    StartRow = LastRow + 1
Loop
ExitLoop:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM