假设列A
中有100
行。 90
其中包含内容,行100
是列A
(低于100
)中所有单元格的总和,不包括9
空单元格。Excel VBA:如何找到带*内容*的*倒数第二*行?
现在,假设秒最后一行与内容是95
行。如何在编写VBA代码时找到它?
我知道如何找到最后行的内容。它的代码是:
LastCellA = .Cells(.Rows.Count, "A").End(xlUp).Row
只是减去1
不会做的工作,因为你看到的。那么,解决方案是什么?
假设列A
中有100
行。 90
其中包含内容,行100
是列A
(低于100
)中所有单元格的总和,不包括9
空单元格。Excel VBA:如何找到带*内容*的*倒数第二*行?
现在,假设秒最后一行与内容是95
行。如何在编写VBA代码时找到它?
我知道如何找到最后行的内容。它的代码是:
LastCellA = .Cells(.Rows.Count, "A").End(xlUp).Row
只是减去1
不会做的工作,因为你看到的。那么,解决方案是什么?
这将找到的倒数第二个非空单元格
LastCellA = .Cells(.Rows.Count, "A").End(xlUp).Row
SecondLastCellA = LastCellA - 1
Do While .Cells(SecondLastCellA, "A") = ""
SecondLastCellA = SecondLastCellA - 1
Loop
mybottom = .Cells(.Rows.Count, "A").End(xlUp).Row
lastcellA = .cells(mybottom -1,"A").end(xlup).row
试试这个
lr = .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row - 1, 1).End(xlUp).Row
与其他答案相同的问题,除非数据中断,否则返回首行数据。 – tjb1
这是我会怎么做:
Function findSecondLastRow(sht As Excel.Worksheet, colLetter As String) As Long
Dim LastCell As Long, SecondLastCell As Long
With sht.Columns(colLetter)
LastCell = .Range(colLetter & .Rows.Count).End(xlUp).Row
If .Range(colLetter & LastCell - 1) = vbNullString Then
SecondLastCell = .Range(colLetter & LastCell).End(xlUp).Row
Else
SecondLastCell = LastCell - 1
End If
End With
findSecondLastRow = SecondLastCell
End Function
调用函数通过作为参数传递工作表参考和le搜索栏,例如:
Debug.Print findSecondLastRow(ActiveSheet, "A")
你可以试试这个
假设你是一个数字常量值感兴趣的最后一个单元格的tter(即没有文字或没有公式)
Function MyLastCellRow(ws As Worksheet, colIndex As Long) As Long
With ws.Columns(colIndex).SpecialCells(xlCellTypeConstants, xlNumbers)
With .Areas(.Areas.Count)
MyLastCellRow = .cells(.Rows.Count).Row
End With
End With
End Function
如果你有兴趣与常值(即文本和/或数字而不是从公式),那么最后一个单元格只是采取, xlNumbers
开来SpecialCells(xlCellTypeConstants, xlNumbers)
如果没有空白,这不起作用。在我的测试工作表中,我用数字1-10填充1-10行。你可以很容易找到第10行,但下一个找到第1行,因为没有休息。 – tjb1