Find the last not empty row in a range of cells holding a formula

后端 未结 4 1486
轮回少年
轮回少年 2021-01-12 03:11

How can I find the last row in a range of cells that hold a formula, where the result of the formula is an actual value and not empty?

Say in a simplified way that

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-12 03:49

    You want to find the last cell in a column that is not empty AND is not a blank string("").

    Just follow the LastRow with a loop checking for a non-blank cell.

    lastrow = ActiveSheet.Range("E" & ActiveSheet.Rows.Count).End(xlUp).Row
    Do
        If ActiveSheet.Cells(lastrow, 5).Value <> "" Then
            Exit Do
        End If
        lastrow = lastrow - 1
    Loop While lastrow > 0
    
    If lastrow > 0 Then
        Debug.Print "Last row with data: " & lastrow
    Else
        Debug.Print "No data in the column"
    End If
    

    Notice that your Rows.count does not specify which sheet. That means it will use the active sheet. Of course ActiveSheet.Range() also is on the active sheet. But it is bad practice to mix Range or Rows with .Range or .Rows. It indicates a thoughtless usage that could bite you if you changed the ActiveSheet but didn't change the unspecified reference.

提交回复
热议问题