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 the range of cells ("E1:E10")
hold a formula referring to cells A1 through A10 as followed =IF("A1"="","","A1")
. But only the cells A1 through A6 have a value filled in, so the result of the formula for cells E7 through E10 will be empty.
Trying to do it with:
lastRow = ActiveSheet.Range("E" & Rows.Count).End(xlUp).Row
results in lastRow having the value of 10
. What I want is for the value of lastRow to be 6
in this example.
The actual code is way more complex than this so I can't just check for the last filled in Row of Column A, as the formulas refer to single cells on different sheets and are added dynamically.
This should help you determine the last row containing a formula (in column
A
on sheet1Sheet1
):SpecialCells
is used to determine the range of all the cells containing a formula. This range is then parsed usingSplit
. WithUbound
the last of these cells is being retrieved. The result is being split again to extract the row number.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.
Notice that your
Rows.count
does not specify which sheet. That means it will use the active sheet. Of courseActiveSheet.Range()
also is on the active sheet. But it is bad practice to mixRange
orRows
with.Range
or.Rows
. It indicates a thoughtless usage that could bite you if you changed theActiveSheet
but didn't change the unspecified reference.I think that more elegant way than was provided by
@D_Bester
is to usefind()
option without looping through the range of cells:test
Also, more shorter version of the code which was provided above is: