我有一个工作表,其中包含从单元格
B3
开始的自动筛选范围。 A
列包含一些宏按钮,但实际上是空白的。前两行包含有关主范围内数据的信息。
在 VBA 中,我使用我认为是确定工作表中最后一行的标准方法(在这种情况下,我不能依赖单列上的
.End
方法):
LastRow = Activesheet.Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row
但是,有时这会返回值 1,即使我有数千行数据。似乎只有在设置了过滤器时才会执行此操作(但仍然存在包含数据的可见行),但即使如此,它也并不总是发生,而且我看不到它的模式。
我知道还有其他解决方案 - 我已经改用一种
UsedRange
技术,但令人非常沮丧的是,这个特定的解决方案失败了,因为否则在这种情况下它可能是最有效的解决方案。
有谁知道为什么会发生这种情况?
您是否想过使用格雷格的答案,但循环查找所有列的最高行?比如:
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
此解决方案允许在底行中随机填充空白值。 usedRange 很棘手,因为它将返回曾经编辑过的最远的外围行/列(即使当前为空白)。根据我的经验,如果您在工作表中按 Ctrl-Up,Range.End(xlUp) 的行为与您所期望的一样。这更容易预测一些。
如果您决定使用 .Find ,请尝试查看 After:=[A1] 参数。我还没有探索这个函数的特性,但是考虑到这个问题,这将是我开始的地方。
试试这个...
Dim LastRow as long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
这将获得 A 列的最后一行。
建议您尝试使用
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”功能不稳定。
但是,经过一段时间的摆弄,我在工作表中的行号太深的地方发现了一个非空单元格,认为它是 1,000 或 10,000 或类似的值。我删除了它,“.find”又可以工作了。可能某些内部 VBA 变量的限制不够大。
这样做:
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
hth 菲利普
我的类似问题是无论是否有过滤之前的空单元格,最后一行和最后一列使用的是什么。我从我能找到的零碎东西中拼凑了这个,它满足了我认为你和我想要的,至少对于填充了数据的单元格来说是这样。
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
并测试:
Sub testit()
Dim ws As Worksheet
Set ws = Application.Worksheets("Sheet1")
cr = FindLastUsedRowAndCol(ws)
MsgBox "row: " & cr(1) & " col: " & cr(2)
End Sub
我知道这是一篇旧帖子,但我确实看到了这个问题,但没有看到任何处理该问题的答案。有时似乎会在数据集上发生这种情况,其中在最后一行之后立即隐藏了行。无论您设置为查找 xlformulas 还是 xlvalues 都没关系,我已经尝试了我能找到的 find 命令的所有排列,并且它始终返回值 1。(正如 OP 所说)上面的解决方案不能解决此问题。我必须创建一个函数,在这种情况下迭代以找到最后一行(下面的关键代码位 - 在我的情况下,我需要在各种数据表的前两列中找到最后一行):
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:
Function LastRow(Optional SpecificCol, Optional TargetShtName) As Long
Dim ws As Worksheet, LRow, temp_LRow As Long
If IsMissing(TargetShtName) Then
Set ws = ActiveSheet
Else
Set ws = Sheets(TargetShtName)
End If
With ws
temp_LRow = Split(Split(.UsedRange.Address, ":")(1), "$")(2) + 0
If IsMissing(SpecificCol) Then
For LRow = temp_LRow To 1 Step -1
If WorksheetFunction.CountA(.rows(LRow)) <> 0 Then Exit For
Next LRow
Else
For LRow = temp_LRow To 1 Step -1
If WorksheetFunction.CountA(.Cells(LRow, columns(SpecificCol).Column)) <> 0 Then Exit For
Next LRow
End If
LastRow = LRow
End With
End Function