Determine last non-value (may have a formula) row in column

前端 未结 4 1409
一向
一向 2021-01-22 13:59

I have a column that has a formula in each row field. The formula propagates data from another Excel spreasheet. If there is nothing in the row field, though, the row remains bl

4条回答
  •  無奈伤痛
    2021-01-22 14:33

    Here's a simple way to find the last cell in a column that does not contain a formula. It will be 0 if there is no cell without a formula.

    Sub Test()
    
    Dim i As Long, tempLast As Long, lastRow As Long
    tempLast = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = tempLast To 1 Step -1
        If Len(Cells(i, 1)) <> 0 Then
            If Not Cells(i, 1).HasFormula Then
                lastRow = i
                Exit For
            End If
        End If
    Next
    
    MsgBox lastRow
    End Sub
    

    Note that you should use "rows.count" and not 65536 as that is no longer the last row in the newer versions of Excel. Rows.count work no matter the version, or user settings. Usedrange should also be avoided since there is a weird bug where you need to refresh the usedrange or you'll get erroneous results.

提交回复
热议问题