Thursday, 15 March 2018

Excel VBA to determine last non-value (IE may have a formula but no value) row in column



I have a column that has a formula in each row field. The formula propagates data from another excel spreasheet. If there's nothing in the row field, though, the row remains blank.



I have found many examples on google to get the last row in a column. However, they fail, because they detect the formula as the row having something in it. That makes sense. However, how can I get the last row in a column that ignores the formula and only attempts to detect values in the column fields?



I'm currently using two methods for searching for the last row in a column of which both fail:




Function lastRowA(rngInput As Range) As Variant

Dim WorkRange As Range
Dim i As Integer, CellCount As Integer
Application.Volatile
Set WorkRange = rngInput.Rows(1).EntireRow
Set WorkRange = Intersect(WorkRange.Parent.UsedRange, WorkRange)
CellCount = WorkRange.Count
For i = CellCount To 1 Step -1
If Not IsEmpty(WorkRange(i)) Then
lastRowA = WorkRange(i).Value
Exit Function

End If
Next i
End Function


and




function lastRow(column as string, optional plusOne as boolean)
If (plusOne = False) then

plusOne=False
End If

if (plusOne = False) Then
lastRow = Replace(Range(column & "65536").End(xlUp).Address, "$", "")
Else
lastRow = Range(column & "65536").End(xlUp).Address
lastRow = Cells(lastRow)
' Replace(, "$", "")
End If

End Function

Answer



If you want to find the last row that contains a non-blank value (either produced by a formula or by entering a constant) try this



Sub FindLastValue()
Dim jLastRow As Long
jLastRow = ActiveSheet.Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End Sub


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...