I decided a function for determining the LastRow in excel might save me some time in the long run thus:
Function LastRow() As Long
Application.Volatile
With ActiveSheet.UsedRange
LastRow = .Rows(.Rows.Count).Row
End With
End Function
For the most part this is fine, but I notice that if the actual last cell is merely formatted it will not recalculate the way it does if a value is entered.
It will get it right on a recalculation, but I thought making this volatile would automatically do that.
What have I misunderstood?
note
anyone doing something similar, be aware that making this volatile and using ActiveSheet makes it unreliable the moment you activate another sheet, as the function returns the LastRow in that sheet and recalculates. Probably not the best idea.
Volatile only triggers when a cell is calculated: MSDN
There is a way to cheat – this is based on the fact that to change a cell, you have to select it first:
Note that this may break some basic functionality. In the Microsoft KB article, they comment that their example will break the ability to copy/paste